Java Program to Convert the LinkedList into an ArrayList

Java Program to Convert the LinkedList into an ArrayList

What is LinkedList ?

In this Article, We will going to write a Program to Convert the LinkedList into an ArrayList.

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 ArrayList:

  1. Create an empty array to store the elements of the linked list.
  2. Traverse the linked list from the head to the tail.
  3. At each node, append the value of the node to the end of the array.
  4. Once you reach the end of the linked list, return the array.

Example:

Run
// Importing all the required packages
import java.util.ArrayList;

public class Main{

    // Node class for Linkedlist creation
    static class Node {
        int data;
        Node next;
        
        Node(int data) {
            this.data = data;
            this.next = null;
        }
    }

    // Function to convert linkedlist to arraylist
    static ArrayList linkedListToArray(Node head) {
        ArrayList result = new ArrayList();
        Node current = head;
        
        // Loop to append the value from LL to AL
        while (current != null) {
            result.add(current.data);
            current = current.next;
        }
        return result;
    }

    public static void main(String[] args) {
        // Adding elements to linkedlist
        Node head = new Node(1);
        head.next = new Node(2);
        head.next.next = new Node(3);
        ArrayList result = linkedListToArray(head);
        System.out.println(result);
    }
}
Output:

[1, 2, 3]

Example using toArray() Function:

Run
// Importing all the required packages
import java.util.LinkedList;

public class Main{
    public static void main(String[] args){
        // Initializng an ArrayList
        LinkedList linkedList = new LinkedList();
        
        // Adding elements to linkedlist
        linkedList.add(1);
        linkedList.add(2);
        linkedList.add(3);

        // Converting linkedlist to ArrayList using toArray function
        Integer[] array = linkedList.toArray(new Integer[linkedList.size()]);

        System.out.println("Linked list: " + linkedList);
        System.out.println("Array: " + Arrays.toString(array));
    }
}
Output:

[1, 2, 3]

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

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription