  Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email  Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

# Program to convert ArrayList to LinkedList in Java

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. 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 : 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)
{

for (String t : 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);

}
}
```

#### 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)
{

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);

}
}
```

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

### Code Implementation

```import java.util.*;
import java.util.stream.*;

class ArrayListToLL {

public static <t> List<t> convertALtoLL(List<t> 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);

}
}
```

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

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.

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

}

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);

}
}
```

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