### Introduction

The linked list is one of the most important 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.

The add() method adds an element to the list. There are two variations of the LinkedList add() method. Let us have a glance at the approaches of both of them.

### Approach and Algorithm

In this method, we pass an object X as a parameter to this function. As a result, this function appends the object element passed to the end of the list.

Note: It returns true only if the action of append is performed.

#### Code Implementation

```

import java.io.*;

public static void main(String args[]) {

System.out.println("The list is:" + list);

System.out.println("The new List is:" + list);
}
}
```

#### Output

The list is:[Coding, is, Fun, 10, 20]
The new List is:[Coding, is, Fun, 10, 20, Computer]

Time Complexity: O(1), as the new object is being added to the end of the list, which takes constant time.
Space Complexity: O(n), the number of object elements that have to be added.

### Approach and Algorithm

In this method, we pass a Object X and an index as parameters to this function. As a result, this function appends the element passed to the specified index that is passed as a parameter.

Note: No value is returned by this method.

#### Code Implementation

```

import java.io.*;

public static void main(String args[]) {

System.out.println("The list is:" + list);

//takes index and a object as parameter

System.out.println("The new List is:" + list);
}
}
```

### Output

The list is:[Coding, is, Fun, 10, 20]
The new List is:[Coding, is, Super, Fun, 10, 20]

Time Complexity: O(n), as we have to traverse the given index to add the element.
Space Complexity: O(n), where n is the number of elements to be added.

So, in this article, we have tried to explain how to use LinkedList add() method in Java. Java Collections is an important topic when it comes to coding interviews. If you want to solve more questions on Linked List, which are curated by our expert mentors at PrepBytes, you can follow this link Linked List.