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

LinkedList addAll() Method in Java

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

Introduction

LinkedList is a linear data structure that is used to store the data. There is a very famous and useful method for LinkedList which is addAll() method. This method is basically used to add data from any Collection to our LinkedList.

There are two ways to apply this method.

Let us discuss each of them:-

1) addAll(Collection X)

Using the above way we can add the data of any Collection at the end of the LinkedList, in the same manner, they were in the Collections.

Syntax: boolean addAll(Collection X)

The above method returns true if at least one 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<Integer> linkedList = new LinkedList<>();
      linkedList.add(1);
      linkedList.add(2);
      linkedList.add(3);
      
      Collection<Integer> list = new ArrayList<>();
      for(int i = 4; i <= 6; i++){
          list.add(i);
      }
      
      System.out.println("The LinkedList is: " + linkedList);
      
      linkedList.addAll(list);
      
      System.out.println("The new linked list is: " + linkedList);
  }
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

2) addAll(int index, Collection X)

Using the above way we can add the data of any Collection at the particular index of the LinkedList, in the same manner, they were in the Collections.

Syntax: boolean addAll(int index, Collection X)

The above method returns true if at least one 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<Integer> linkedList = new LinkedList<>();
      linkedList.add(1);
      linkedList.add(2);
      linkedList.add(3);
      
      Collection<Integer> list = new ArrayList<>();
      for(int i = 4; i <= 6; i++){
          list.add(i);
      }
      
      System.out.println("The LinkedList is: " + linkedList);
      
      linkedList.addAll(1, list);
      
      System.out.println("The new linked list is: " + linkedList);
  }
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

Time Complexity: The time complexity for both of the above functions is O(N)(where N is the total number of elements in the Collection’s list to be added). 

Space Complexity: Both of the above functions does not use any auxiliary space internally. Hence, the space complexity is O(1).

Read More - Time Complexity of Sorting Algorithms

Frequently Asked Questions

What are the two types of addAll() method we can use?

The two types of addAll method are addAll(Collection X) used to add all the elements of a Collection at the end of the LinkedList. The other is addAll(int index, Collection X) that is used to add the data of any Collection at the particular index of the LinkedList.

What is the time complexity for both functions?

The time complexity for both of the above functions is O(N)(where N is the total number of elements in the Collection’s list to be added). 

 

Conclusion

In this blog, we have covered the following things:

  1. We first discussed the addAll() method of LinkedList in Java.
  2. Then we discussed the two types of addAll 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.

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, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

If you want to learn more about Linked List and want to practice some questions which require you to take your basic knowledge on  Linked List a notch higher, then you can visit our Guided Path for  Linked List

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

Live masterclass