Last Updated on December 13, 2022 by Prepbytes
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 addAll() method in Java.
The addAll() method add a collection to the list. There are two variations of the LinkedList addAll() method. Let us have a glance at the approaches of both of them.
Approach and Algorithm (addAll(Collection X))
LinkedList.addAll(Collection X) : In this method, we pass a Collection X as a parameter to this function. As a result, this function appends all the elements of the collection passed to the end of the list and also while appending it keeps in mind the order of return by the collection’s iterator.
Syntax : boolean addAll(Collection X)
It returns true only if at least a single action of append is performed.
Code Implementation
import java.util.*; import java.util.LinkedList; import java.util.ArrayList; class LinkedListDemo { public static void main(String args[]) { LinkedList<String> list = new LinkedList<String>(); list.add("Coding"); list.add("is"); list.add("Fun"); list.add("10"); list.add("20"); Collection<String> collect = new ArrayList<String>(); collect.add("A"); collect.add("Computer"); collect.add("Game"); System.out.println("The LinkedList is: " + list); list.addAll(collect); System.out.println("The new linked list is: " + list); } }
Output
The LinkedList is: [Coding, is, Fun, 10, 20]
The new linked list is: [Coding, is, Fun, 10, 20, A, Computer, Game]
Time Complexity: O(n), where n is the size of the collection to be added.
Space Complexity: O(n), where n is the size of the collection to be added.
Approach and Algorithm (addAll(int index,Collection X))
LinkedList.addAll(int index,Collection X) : In this method, we pass a Collection X and an index as parameters to this function. As a result, this function appends all the elements of the collection to the list at a specified index of the list that is passed as a parameter.
Syntax : boolean addAll(int index, Collection X)
It returns true only if at least a single action of append is performed.
Code Implementation
import java.util.*; import java.util.LinkedList; import java.util.ArrayList; public class LinkedListDemo { public static void main(String args[]) { LinkedList<string> list = new LinkedList<string>(); list.add("Coding"); list.add("is"); list.add("10"); list.add("20"); Collection<string> collect = new ArrayList<string>(); collect.add("PrepBytes"); collect.add("Placement"); collect.add("Program"); collect.add("for"); collect.add("Students"); System.out.println("The LinkedList is: " + list); list.addAll(1, collect); System.out.println("The new linked list is: " + list); } }
Output
The LinkedList is: [Coding, is, 10, 20]
The new linked list is: [Coding, PrepBytes, Placement, Program, for, Students, is, 10, 20]
Time Complexity: O(n), where n is the size of the collection to be added.
[forminator_quiz id=”4042″]
So, in this article, we have tried to explain how to use LinkedList addAll() 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.