Introduction
LinkedList is one of the most important concepts to learn while preparing for interviews. In a coding interview, having a good understanding of LinkedList can be a huge advantage. So, to help you in leaving no stone unturned, we are here with an exciting question.
In this article, we are going to learn how to convert an ArrayList to LinkedList Data Structure. So, without any further ado, let's get started!
What is an ArrayList?
ArrayList is a list interface implementation that allows elements to be dynamically added or removed from the list. In addition, if the number of elements added exceeds the initial number, the list's size is dynamically increased.
What is a LinkedList?
A LinkedList is a collection of data elements that are not stored in memory in a contiguous manner. Each data element has a pointer to the next data element in the collection. A node is a term used to describe any element.
Recommended Topic, Floyds Algorithm And Rabin Karp Algorithm
Different approaches to converting ArrayList to LinkedList
There are different approaches in java to convert an ArrayList to a LinkedList. Let us understand these methods one by one:
1. Using Brute Force Method
A brute force method is a simple approach that comes to our mind initially. In this method, we create an empty LinkedList and add all of the elements from the ArrayList one by one.
Algorithm to solve the question:
Step 1: Get the ArrayList that has to be converted.
Step 2: Make a LinkedList that is empty.
Step 3: Iterate through the ArrayList's items.
Step 4: Add each item to the LinkedList.
Step 5: Return the LinkedList that was created.
Java Code:
import java.util.*;
import java.util.stream.*;
class ArrayListToLinkedList{
// function to convert an ArrayList to LinkedList
public static <T> List<T> convertArrtoLinkedList(List<T> arr)
{
// empty LinkedList
List<T> list = new LinkedList<>();
// Iterate through the arr
for (T x : arr) {
// Add each element into the list
list.add(x);
}
// Return the converted LinkedList
return list;
}
public static void main(String args[])
{
// Create an ArrayList
List<String> arr = Arrays.asLists("Coding",
"Ninjas",
"Excel");
// Print the ArrayList
System.out.println("ArrayList: " + arr);
// convert the ArrayList to LinkedList
List<String>
list = convertArrtoLinkedList(arr);
// Print the LinkedList
System.out.println("LinkedList: " + list);
}
}
Output:
ArrayList: [Coding,Ninjas,Excel]
LinkedList: [Coding,Ninjas,Excel]
Time Complexity: O(n) is the time required to do the traversal.
Space Complexity: O(n) is the space required to create a LinkedList.
2. Using List Constructor
In this method, we will pass the ArrayList as a parameter to the LinkedList constructor.
Algorithm to solve the question:
Step 1: Get the ArrayList that has to be converted.
Step 2: We need to create a LinkedList on passing the ArrayList as a parameter to the LinkedList.
Step 3: Return the LinkedList that was created.
Java Code:
import java.util.*;
import java.util.stream.*;
class ArrayListToLinkedList{
// Function to convert an ArrayList to a LinkedList
public static <T> List<T> convertArrtoLinkedList(List<T> arr)
{
// Create the LinkedList by passing the ArrayList
List<T> list = new LinkedList<>(arr);
// Return the converted LinkedList
return list;
}
public static void main(String args[])
{
// Create an ArrayList
List<String> arr = Arrays.asList("Coding",
"Ninjas",
"Excel");
// Print the ArrayList
System.out.println("ArrayList: " + arr);
// converting the ArrayList to LinkedList
List<String>
list = convertArrtoLinkedList(arr);
// Printing the LinkedList formed
System.out.println("LinkedList: " + list);
}
}
Output:
ArrayList: [Coding,Ninjas,Excel]
LinkedList: [Coding,Ninjas,Excel]
Time Complexity: O(n) is the time required to do the list traversal.
Space Complexity: O(n) is the space required to create the LinkedList.
3. Using Java 8 Stream API
In this method, we will use Stream.collect(). We will convert the ArrayList to a stream and collect the elements of the stream into a LinkedList.
Algorithm to solve the question:
Step 1: Get the ArrayList that has to be converted.
Step 2: We need to create a stream from the ArrayList.
Step 3: We have to collect the ArrayList Stream and convert it to LinkedList using Collectors.
Step 4: Then, gather the LinkedList.
Step 5: Return the LinkedList that was created.
Java Code:
import java.util.*;
import java.util.stream.*;
class ArrayListToLinkedList {
// function to convert an ArrayList to LinkedList
public static <T> List<T> convertArrtoLinkedList(
List<T> arr)
{
return arr
.stream()
.collect(Collectors
.toCollection(LinkedList::new));
}
public static void main(String args[])
{
// Create an ArrayList
List<String> arr = Arrays.asList("Coding",
"Ninjas",
"Excel");
// Print the ArrayList
System.out.println("ArrayList: " + arr);
// convert the ArrayList to LinkedList
List<String> list = convertArrtoLinkedList(arr);
// Print the LinkedList
System.out.println("LinkedList: " + list);
}
}
Output:
ArrayList: [Coding,Ninjas,Excel]
LinkedList: [Coding,Ninjas,Excel]
Time Complexity: O(n) is the time required to do list traversal.
Space Complexity: O(n) is the space required to create LinkedList.
4. Google’s Guava library Method
The Collection.addAll() method in Guava provides a LinkedList implementation that can be used to create a LinkedList from another collection.
Algorithm to solve the question:
Step 1: Get the ArrayList that has to be converted.
Step 2: We have to create an empty LinkedList.
Step 3: Using LinkedList.addAll(), add the elements of the ArrayList to the LinkedList, and pass ArrayList as a parameter.
Step 4: Return the LinkedList that was created.
Java Code:
import java.util.*;
import java.util.stream.*;
class ArrayListToLinkedList {
// function for converting ArrayList to LinkedList
public static <T> List<T> convertArrtoLinkedList(List<T> arr)
{
List<T> list = new LinkedList<>();
// Add ArrayList to the list
list.addAll(arr);
// Return converted LinkedList
return list;
}
public static void main(String args[])
{
// Create an ArrayList
List<String> arr = Arrays.asList("Coding",
"Ninjas",
"Excel");
// Print the ArrayList
System.out.println("ArrayList: " + arr);
// convert the ArrayList to LinkedList
List<String>
list = convertArrtoLinkedList(arr);
// Print the LinkedList
System.out.println("LinkedList: " + list);
}
}
Output:
ArrayList: [Coding,Ninjas,Excel]
LinkedList: [Coding,Ninjas,Excel]
Time Complexity: O(n) is the time required to do list traversal.
Space Complexity: O(n) is the space required to create LinkedList.
You can also read Difference Between Array List and Linked List here.
Frequently Asked Questions
What is the basic difference between an ArrayList and a LinkedList?
Internally, ArrayList stores its elements in a dynamic array. The elements of LinkedList are stored in a Doubly Linked List. ArrayList takes a long time to load because array manipulation takes a long time. Because node-based LinkedList requires less bit shifting, it is faster.
Why do we use an ArrayList rather than a LinkedList?
Because ArrayList provides a constant time for searching, it is preferable to use it if searching is a more frequent operation than adding and removing items. Add and remove operations on the LinkedList take the same amount of time. For manipulation, it is, therefore, preferable to use LinkedList.
Which is faster, LinkedList or ArrayList?
ArrayList saves data according to indexes and implements the RandomAccess interface, which is a marker interface that gives ArrayList the ability to retrieve data in a random order, whereas LinkedList does not, which is why ArrayList is faster than LinkedList.