Table of contents
1.
Introduction
2.
1) linkedList add(Object X)
3.
2) linkedList add(int index, Object X)
4.
Frequently Asked Questions
4.1.
What are the two types of LinkedList add() methods we can use?
4.2.
What is the time complexity for both functions?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

LinkedList add() Method in Java

Author NISHANT RANA
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?
Linked List

Introduction

Linked List is a linear data structure that is used to store the data. It is a dynamic data structure and can also be considered a custom data type. Linked Lists are one of the easiest Data Structures to start with and have a wide range of applications across various fields. 

There is a very famous and useful method for LinkedList add() method. This method is used to add the data to our LinkedList.

There are two ways to apply this method. Let us discuss each of them:-

Recommended Topic, Floyds Algorithm And Rabin Karp Algorithm

1) linkedList add(Object X)

Using the above method we can add the data at the end of the Linked List.

 

Syntax: boolean add(Object X)

We need to pass the data to this method which we need to add to our LinkedList. This method appends the data at the end of the LinkedList. The above method returns true if the element is appended to the LinkedList.

 

Refer to the below code for more understanding.

 

import java.util.*;
  
public class LinkedListDemo {
  public static void main(String args[]) {
  
      LinkedList<String> list = new LinkedList<>();
  
      list.add("10");
      list.add("20");
      list.add("30");
      list.add("40");
      list.add("50");
  
      System.out.println("The list is:" + list);
  
      list.add("60");
  
      System.out.println("The new List is:" + list);
  }
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

Output

Time Complexity: The time complexity for the above functions is O(1).

Space Complexity: The space complexity for the above method is O(N) to maintain all the ‘N’ elements of the LinkedList.

2) linkedList add(int index, Object X)

Using the above method we can add the data at any particular index of the LinkedList.

Syntax: boolean add(int index, Object X)

The above method returns true if at least one element is appended to the LinkedList at the given index. 

Refer to the below code for more understanding.

 

import java.util.*;
  
public class LinkedListDemo {
  public static void main(String args[]) {
  
      LinkedList<String> list = new LinkedList<>();
  
      list.add("10");
      list.add("20");
      list.add("40");
      list.add("50");
      list.add("60");
  
      System.out.println("The list is:" + list);
  
      list.add(2, "30");
  
      System.out.println("The new List is:" + list);
  }
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

Output

Time Complexity: The time complexity for the above functions is O(N) because we need to traverse to the given index.

Space Complexity: The space complexity for the above method is O(N) to maintain all the N elements of the LinkedList.

 

Frequently Asked Questions

What are the two types of LinkedList add() methods we can use?

The two types of LinkedList add method are LinkedList add(Object X) used to the element at the end of the LinkedList. The other is LinkedList add(int index, Object X) which is used to add the data at the particular index of the LinkedList.

What is the time complexity for both functions?

The time complexity for the first function is O(1), whereas the Time complexity for the second method is O(N) because we need to traverse to the given index.

Conclusion

In this blog, we have covered the following things:

  1. We first discussed the LinkedList add() method in Java.
  2. Then we discussed the two types of LinkedList add methods in Java.


Recommended Problems -

Recommended Reading:

Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Coding Ninjas Studio.

To study more about Linked Lists, refer to Applications Of Linked Lists.

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, Online Java Compiler and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Until then, All the best for your future endeavors, and Keep Coding.

Live masterclass