LinkedList add() method in Java

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.

In this article, we are going to learn how to use the LinkedList add() method in Java.

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

add(Object X) method

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.

Syntax: boolean add(Object X)

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

Code Implementation





import java.io.*;
import java.util.LinkedList;
  
public class LinkedListDemo {
   public static void main(String args[]) {
  
      LinkedList list = new LinkedList();
  
     
      list.add("Coding");
      list.add("is");
      list.add("Fun");
      list.add("10");
      list.add("20");
  

      System.out.println("The list is:" + list);
  
      list.add("Computer");
  
      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

add(int index,Collection X) method

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.

Syntax: void add(int index,Object X)

Note: No value is returned by this method.

Code Implementation




import java.io.*;
import java.util.LinkedList;
  
public class LinkedListDemo {
   public static void main(String args[]) {

      LinkedList list = new LinkedList();
  
      list.add("Coding");
      list.add("is");
      list.add("Fun");
      list.add("10");
      list.add("20");
  
      System.out.println("The list is:" + list);
      
      //Here we are talking about this add method which 
      //takes index and a object as parameter
      list.add(2, "Super");
  

      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.

Previous post Delete a node at a given position
Next post LinkedBlockingQueue size() method in Java

Leave a Reply

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