Java Program to Convert the LinkedList into an Array and vice versa
What is LinkedList ?
In this Article, We will going to write a Program to Convert the LinkedList into an Array and vice versa.
A linked list is a linear data structure in which each element (called a node) stores a reference to the next element in the list. Linked lists are dynamic data structures, which means that their size can change during the execution of a program. In contrast, arrays have a fixed size, which means that you need to specify the size of the array when you create it and you cannot change it later.
Algorithm to Convert the LinkedList into an Array:
- Create an empty array.
- Set the current node of the linked list to the head node.
- Traverse the linked list using a loop until the current node is null.
- At each node, add the node’s value to the end of the array using the array’s “push” method.
- Set the current node to the next node in the linked list.
- When the loop is finished, the array will contain all the values from the linked list.
Pseudo Code:
// function definition function linkedListToArray(head){ // Array Initialization const arr = []; let current = head; // loop to convert linkedlist to array while (current !== null) { arr.push(current.val); current = current.next; } // returning the array return arr; }
Example:
// importing all the required packages import java.util.LinkedList; public class Main{ public static void main(String[] args){ // Create a linked list LinkedListlinkedList = new LinkedList (); // Adding elements to a Linkedlist linkedList.add(10); linkedList.add(20); linkedList.add(30); linkedList.add(40); linkedList.add(50); // Convert the linked list to an array Integer[] array = linkedList.toArray(new Integer[linkedList.size()]); // Print the array for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } } }
Output: 10 20 30 40 50
Algorithm to Convert the Array into an LinkedList:
- Create a new head node and set it to null.
- Loop through the array in reverse order (starting from the last element).
- For each element in the array, create a new node and set its value to the current element.
- Set the “next” pointer of the new node to the current head node.
- Set the new node as the new head node.
- When the loop is finished, the new head node will be the first node in the linked list.
Pseudo Code:
// Function definition function arrayToLinkedList(arr) { Node head = null; // loop to convert array to linkedlist for (int i = arr.length - 1; i >= 0; i--) { Node newNode = new Node(arr[i]); newNode.next = head; head = newNode; } return head; }
Example:
// importing all the required packages import java.util.*; public class Main { public static void main(String[] args) { // Create an integer array int[] arr = {10, 20, 30, 40, 50}; // Convert the array to a linked list Node head = arrayToLinkedList(arr); // Print the linked list printLinkedList(head); } // Function definition public static Node arrayToLinkedList(int[] arr) { Node head = null; for (int i = arr.length - 1; i >= 0; i--) { Node newNode = new Node(arr[i]); newNode.next = head; head = newNode; } return head; } // Print function public static void printLinkedList(Node head) { while (head != null) { System.out.print(head.val + " "); head = head.next; } } } // Node class class Node { int val; Node next; public Node(int val) { this.val = val; } }
Output: 10 20 30 40 50
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment