Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Medium

# Program to Convert ArrayList to LinkedList in Java

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

## 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.*;

// function to convert an ArrayList to LinkedList
public static <T> List<T> convertArrtoLinkedList(List<T> arr)
{

List<T> list = new LinkedList<>();

// Iterate through the arr
for (T x : arr) {

// Add each element into the list
}

// 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>

// Print the LinkedList
System.out.println("LinkedList: " + list);
}
}``````

Output:

``````ArrayList: [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.*;

// 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>

// Printing the LinkedList formed
System.out.println("LinkedList: " + list);
}
}``````

Output:

``````ArrayList: [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.*;

// function to convert an ArrayList to LinkedList
public static <T> List<T> convertArrtoLinkedList(
List<T> arr)
{

return arr
.stream()
.collect(Collectors
}

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]

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.*;

// 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

// 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>

// Print the LinkedList
System.out.println("LinkedList: " + list);
}
}``````

Output:

``````ArrayList: [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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

## Conclusion

In this article, we've learned different methods to convert ArrayList to LinkedList data structure. If you want to learn the differences between ArrayList and LinkedList in detail, you can go here.

Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Thank you for reading!

Topics covered
1.
Introduction
1.1.
What is an ArrayList?
1.2.
What is a LinkedList?
2.
Different approaches to converting ArrayList to LinkedList
2.1.
1. Using Brute Force Method
2.2.
2. Using List Constructor
2.3.
3. Using Java 8 Stream API
2.4.
4. Googleâ€™s Guava library Method
3.