coderolls Staff
coderolls Staff Hi 👋🏼, At coderolls we write tutorials about Java programming language and related technologies.

How To Add Element To LinkedList?

In this article, we will see, how to add an element to LinkedList. The LinkedList class provide various methods to add an element to the LnikedList. We will cover these methods with examples.

Introduction

The LinkedList class is present in the java.util package.

LinkedList internally uses doubly LinkedList for storing the variables. That’s why LinkedList is preferred when our frequent operation is insertion and deletion.

We can add an element to the LinkedList using the add() method. There are two types of the add() method.

  1. add(E e)
  2. add(int index,E element)
  3. addFirst(E e)
  4. addLast(E e)

The add() method will append an element to the end of the list. The add(int index, E element) method will add the element at the specified index index.

Since LinkedList internally use a doubly LinkedList structure to store data, it provides two additional methods to add the elements using addFirst(E e) and addLast(E e)

Let’s see both the methods one by one.

1. add(E e)

The method signature for this is given below,

public boolean add(E e).

This method appends the specified element e to the end of the list.

This method returns true if the element is added to the list otherwise, it returns false.

I have given a Java program to add an element to the end of the list using the add(E e) method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java.util.LinkedList;

/**
 * A Java program to add an element to the LinkedList.
 * 
 * The add(E e) method will add an element to 
 * the end of the LinkedList.
 * 
 * @author coderolls.com
 *
 */
public class LinkedListAddExample {
  public static void main(String[] args) {
    LinkedList<String> linkedList = new LinkedList<String>();
    
    //Adding elements to the LinkedList
    linkedList.add("John");
    linkedList.add("Peter");
    
    System.out.println(linkedList);// [John, Peter]
    
    //Adding an element to the LinkedList
    //Element will be added to the end of the LinkedList
    linkedList.add("Noah");
    
    System.out.println(linkedList); // [John, Peter, Noah]
  }
}

Output:

1
2
[John, Peter]
[John, Peter, Noah]

2. add(int index, E element)

The method signature for this method is given below.

1
public void add(int index, E element)

This method adds the specified element element at the specified index index.

This method has a return type as void so, it will not return anything.

Below, we will see a Java program to add an element at the specified index in LinkedList.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.util.LinkedList;

/**
 * A Java program to add an element to the LinkedList
 * at the specific index.
 * 
 * The add(int index, E element) method will add 
 * an element at the specified index in LinkedList.
 * 
 * @author coderolls.com
 *
 */
public class LinkedListAddExample2 {

  public static void main(String[] args) {
    LinkedList<String> linkedList = new LinkedList<String>();
    
    //adding elements to the linkedList using the normal add method
    linkedList.add("John");
    linkedList.add("Peter");
    linkedList.add("Karan");
    
    System.out.println(linkedList);// [John, Peter, Karan]
    
    // adding an element to  the LinkedList at index 1
    linkedList.add(1, "Noah");
    
    System.out.println(linkedList); // [John, Noah, Peter, Karan]
  }
}

Output:

1
2
[John, Peter, Karan]
[John, Noah, Peter, Karan]

3. addFirst(E e)

We know that LinkedList internally uses the doubly linkedlist structure to store the data. To provide features like a doubly LinkedList, the LinkedList class has a special addFirst(E e) method to add the element at the beginning of the list.

I have written a separate detailed article about the addFirst(E e) method. Read How To Add Element At The Beginning Of LinkedList?

4. addLast(E e)

We know that LinkedList internally uses the doubly linkedlist structure to store the data. To provide features like a doubly linkedlist, the LinkedList class has a special addLast(E e) method to add the element to the end of the list.

This method is equivalent to the add(E e) method.

I have written a separate detailed article about the addLast(E e) method. Read LinkedList addLast(E e) Method

Conclusion

We can add an element to the LinkedList using the add() method in two ways.

  1. add(E e) - It will add an element to the end of the LinkedList
  2. add(int index, E element)- This method will add an element to the specified index
  3. addFirst(E e) - This method will add an element at the beginning of the list
  4. addLast(E e) - This method will add an element to the end of the list

The example Java programs given in the above tutorial can be found at this GitHub Repository.

Let me know your thoughts or suggestions on the topic discussed above in the comment section below.

Join Newsletter
Get the latest tutorials right in your inbox. We never spam!

comments powered by Disqus