Linked List – add method in Java

add() method is used to add an element within the Linked List. In this post, we will discuss the add method of the java Linked List class.

There are two add overloaded add method implementations in Linked List, which are listed below –

public boolean add(E e) –

  • What does it do? – It will accept an element e as an argument and append it to the list.
  • Now, what is element means here? – An element will be any object you want to add at the end of the list. It can be an integer, string, custom-made object, etc.
  • What will it return? – It will return true after executing it successfully.

We will make a linked list and try to use the add method. So, let’s dive into it –

import java.util.LinkedList;

public class Codekru {

	public static void main(String[] args) {
		LinkedList<String> linkedList = new LinkedList<String>();
		linkedList.add("First");
		linkedList.add("Second");
		
		System.out.println("Current Linked List: " + linkedList);
		
		linkedList.add("Third");
		
		System.out.println("After using add method: "+linkedList);
	}
}

Output –

Current Linked List: [First, Second, 10]
After using add method: [First, Second, 10, Third]
Time Complexity of LinkedList add(E e) method

Java Linked List keeps track of the first and last node. So, the below steps are done to add a new node –

  • The new node( number 4 in the image below) will now be the last node in the Linked list.
  • The previous last node ( number 3 in the below image ) will be pointing to the newly formed node.
  • And the newly formed node ( number 4 ) will also point to the previous last node ( number 3 )

And it is done in O(1) time complexity. So, the time complexity of the add(E e ) method would be O(1).

adding an element to list using add() method

public void add(int index, E element)

  • What does it do? – It will add a node at the mentioned index passed in the arguments. Remember, the indexing will be 0-based. So, if you have to insert at 3rd position, then pass 2 as an index in the method’s argument.
  • What will it return? – It will return nothing but will throw IndexOutOfBoundsException if you have passed an index that is going out of bounds from the size of the linked list (or when index > size of the linked list).
import java.util.LinkedList;

public class Codekru {

	public static void main(String[] args) {
		LinkedList<String> linkedList = new LinkedList<String>();
		linkedList.add("First");
		linkedList.add("Second");
		linkedList.add("Fourth");

		System.out.println("Current Linked List: " + linkedList);

		linkedList.add(2, "Third");    // adding an element at positon 3 by passing index as 2

		System.out.println("After using add method with index: " + linkedList);
	}
}

Output –

Current Linked List: [First, Second, Fourth]
After using add method with index: [First, Second, Third, Fourth]
Time Complexity of LinkedList add(int index, E element) method
  • Best Case Complexity – Here, the best case would be when we have to add a node at the beginning or end of the list, which can be done in O(1) time complexity.
  • Worst Case Complexity – In this method’s implementation, we can see that Java will add a node with respect to the first or last node, whichever may be the closest to the index. So, the worst case would be when we will try to add a node right in the middle. So, it will have a complexity of O(n/2).

Please visit this link to know more about the linked list and its other functions or methods.

We hope that you have liked the article. Please feel free to write us in the comments or mail us at [email protected] for any doubts or concerns.

Related Articles –

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *