# Program to convert ArrayList to LinkedList in Java

### Introduction

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

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

class ArrayListToLL {

public static  List convertALtoLL(List aL)
{

for (T t : aL) {

}

return lL;
}

public static void main(String args[])
{
List aL = Arrays.asList("Games",
"On",
"A computer Portal");

System.out.println("ArrayList: " + aL);

List
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  List convertALtoLL(List aL)
{

return lL;
}

public static void main(String args[])
{

List aL = Arrays.asList("Games",
"On",
"A computer Portal");

System.out.println("ArrayList: " + aL);

List
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  List convertALtoLL(
List aL)
{

return aL

.stream()

.collect(Collectors

}

public static void main(String args[])
{

List aL = Arrays.asList("Games",
"On",
"A computer Portal");

System.out.println("ArrayList: " + aL);

List  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  List convertALtoLL(List aL)
{

return lL;
}

public static void main(String args[])
{

List aL = Arrays.asList("Games",
"On",
"A computer Portal");

System.out.println("ArrayList: " + aL);

List
lL = convertALtoLL(aL);