Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

LinkedList add() method in Java

Last Updated on December 13, 2022 by Prepbytes

In the article, we will learn the linked list add method in java. A linked list is the link of nodes connected to every next node through a pointer and it’s one of the best data structures for a beginner to start with and also has a good variety of coding questions.let try to understand the linked list 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 of the linked list add method in java

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 of the linked list add method in java


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 of the linked list add method in java

add(int index, Collection X) method

In this method, we pass an 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 of the linked list add method in java

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 explained how to use linked list add method in java. Java Collections is an important topic when it comes to coding interviews and we have first explained Linked list add() method and two different approaches with code implementation as well. If you want to solve more questions on Linked List you can follow this link Linked List.

FAQs related to linked list add method in java

1. Which is faster Linked List or Array List?
Linked list is faster as being node based not much shifting is required and also implements list as well as queue where Array List only implements list.

2. What is the time complexity of of method add(Object X) in linked list using java?
The time complexity used is O(1).

3. How do you sum two strings in java ?
Java uses the + operator for both addition and concatenation

Leave a Reply

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