Last Updated on November 24, 2022 by Prepbytes
The linked list is one of the most important concepts and data structures to learn while preparing for interviews. Having a good grasp of Linked Lists can be a huge plus point in a coding interview.
Problem Statement
In this question, we are given an ArrayList. We have to convert this ArrayList to a LinkedList.
Problem Statement Understanding
Suppose the given ArrayList is [Games, On, A Computer].
We have to convert this ArrayList to LinkedList.
The resultant LinkedList will be:
An ArrayList is a part of the collection framework and is present in java. util package. It helps us to create dynamic lists. Though it may be a bit slower than arrays but can be of great use where lots of manipulating operations are needed in an array.
Input : ArrayList – [Games, On, A Computer]
Output :
Linkedlist
Explanation : We can see that the given ArrayList has been converted to a LinkedList.
This question is not a very complex one. We have to make use of ArrayList traversal to solve it. Let us have a glance at the multiple approaches.
Approach and Algorithm (Naive Solution)
The approach is going to be pretty simple.
- We are going to create a new empty LinkedList.
- Then, we will simply traverse through the ArrayList and one by one, insert that element into the LinkedList.
Code Implementation
/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; class ArrayListToLL { public static List<String> convertALtoLL(List<String> aL) { List<String> lL = new LinkedList<>(); for (String t : aL) { lL.add(t); } return lL; } public static void main(String args[]) { List<String> aL = Arrays.asList("Games", "On", "A computer Portal"); System.out.println("ArrayList: " + aL); List<String> lL = convertALtoLL(aL); System.out.println("LinkedList: " + lL); } }
Output
ArrayList : [Games, On, A computer]
LinkedList : [Games, On, A computer]
Time Complexity: O(n), as list traversal is needed.
Space Complexity: O(n), the space to create the LinkedList.
Approach and Algorithm (Plain Java)
In this approach, we will pass the ArrayList as the parameter into the LinkedList constructor.
- We will create an empty LinkedList.
- Then we will pass the ArrayList as a parameter to the LinkedList constructor. It will then create the required LinkedList.
Code Implementation
import java.util.*; import java.util.stream.*; class ArrayListToLL { public static <t> List<t> convertALtoLL(List<t> aL) { List<t> lL = new LinkedList<>(); lL.addAll(aL); return lL; } public static void main(String args[]) { List<string> aL = Arrays.asList("Games", "On", "A computer Portal"); System.out.println("ArrayList: " + aL); List<string> lL = convertALtoLL(aL); System.out.println("LinkedList: " + lL); } }
Output
ArrayList : [Games, On, A computer]
LinkedList : [Games, On, A computer]
Time Complexity: O(n), as list traversal is needed.
Space Complexity: O(n), the space to create the LinkedList.
Approach (Java 8 Stream API)
In this approach, we are going to convert the ArrayList to a stream and collect the elements of the stream in a LinkedList using the method Stream.collect().
Algorithm
- Convert the ArrayList into a stream.
- With the help of collectors, collect the ArrayList stream and convert the collected ArrayList stream into a LinkedList.
- Collect the LinkedList
- Return the LinkedList
Code Implementation
import java.util.*; import java.util.stream.*; class ArrayListToLL { public static <t> List<t> convertALtoLL(List<t> aL) { List<t> lL = new LinkedList<>(aL); return lL; } public static void main(String args[]) { List<string> aL = Arrays.asList("Games", "On", "A computer Portal"); System.out.println("ArrayList: " + aL); List<string> lL = convertALtoLL(aL); System.out.println("LinkedList: " + lL); } }
Output
ArrayList : [Games, On, A computer]
LinkedList : [Games, On, A computer]
Time Complexity: O(n), as list traversal is needed.
Space Complexity: O(n), the space to create the LinkedList.
Approach (Google’s Guava Library)
In this approach, we are going to create the LinkedList from another collection, with the help of Collection.addAll() method.
Algorithm
- Convert the ArrayList into a stream.
- Create an empty LinkedList.
- Adding all the elements of the ArrayList to the LinkedList by passing the ArrayList as the parameter when using LinkedList.addALL() method.
- Return the linked list.
Code Implementation
import java.util.*; import java.util.stream.*; class ArrayListToLL { public static <t> List<t> convertALtoLL( List<t> aL) { return aL .stream() .collect(Collectors .toCollection(LinkedList::new)); } public static void main(String args[]) { List<string> aL = Arrays.asList("Games", "On", "A computer Portal"); System.out.println("ArrayList: " + aL); List<string> lL = convertALtoLL(aL); System.out.println("LinkedList: " + lL); } }
Output
ArrayList : [Games, On, A computer]
LinkedList : [Games, On, A computer]
Time Complexity: O(n), as list traversal is needed.
Space Complexity: O(n), the space to create the LinkedList.
So, in this article, we have tried to explain multiple approaches to convert an ArrayList to LinkedList in Java. This is an important, as this technique is sometimes required in advanced problems. If you want to solve more questions on Linked List, which are curated by our expert mentors at PrepBytes, you can follow this link https://mycode.prepbytes.com/interview-coding/practice/linked-list.
Other Java Programs
Java program to find the factorial of a number
Java program to search an element in a linked list
Java program to add two numbers
Java program to reverse a linked list