Java LinkedList add() Method
The LinkedList class provides multiple overloaded versions of the add() method for adding elements to the list.
1、boolean add(E e)
This is the most basic add method, which appends the specified element to the end of the list.
Example
LinkedList<String> list = new LinkedList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
list.add("Apple");
list.add("Banana");
list.add("Cherry");
Method Characteristics:
- Always returns true (because LinkedList allows duplicate elements)
- Time complexity is O(1)
- Equivalent to the addLast() method
2、void add(int index, E element)
Inserts the specified element at the specified position in the list.
Example
LinkedList<String> list = new LinkedList<>();
list.add("Apple");
list.add("Banana");
list.add(1, "Orange"); // Insert at index 1
list.add("Apple");
list.add("Banana");
list.add(1, "Orange"); // Insert at index 1
Method Characteristics:
- If the index equals the list size, it is equivalent to add(E e)
- Requires traversal to the specified position, with an average time complexity of O(n)
- May throw IndexOutOfBoundsException
3、boolean addAll(Collection<? extends E> c)
Appends all elements in the specified collection to the end of the list in the order returned by its iterator.
Example
LinkedList<String> fruits = new LinkedList<>();
fruits.add("Apple");
fruits.add("Banana");
List<String> moreFruits = Arrays.asList("Orange", "Grape");
fruits.addAll(moreFruits);
fruits.add("Apple");
fruits.add("Banana");
List<String> moreFruits = Arrays.asList("Orange", "Grape");
fruits.addAll(moreFruits);
Method Characteristics:
- Returns true if the list changed as a result of the call
- Time complexity is O(m), where m is the size of the parameter collection
4、boolean addAll(int index, Collection<? extends E> c)
Inserts all elements in the specified collection into this list, starting at the specified position.
Example
LinkedList<String> fruits = new LinkedList<>();
fruits.add("Apple");
fruits.add("Banana");
List<String> moreFruits = Arrays.asList("Orange", "Grape");
fruits.addAll(1, moreFruits); // Insert the collection at index 1
fruits.add("Apple");
fruits.add("Banana");
List<String> moreFruits = Arrays.asList("Orange", "Grape");
fruits.addAll(1, moreFruits); // Insert the collection at index 1
Method Characteristics:
- Requires traversing to the specified position first
- Time complexity is O(n + m), where n is the list size and m is the collection size
Comparison with Other Methods
add() vs addLast()
add(E e)andaddLast(E e)Completely equivalent- Both are used to add elements at the end of the list
add() vs offer()
add(E e)May throw an exception in a capacity-restricted queueoffer(E e)Returns false instead of throwing an exception in a capacity-restricted queue
add() vs push()
push(E e)Actually callsaddFirst(E e)- Adds the element to the beginning of the list instead of the end
Performance Considerations
Efficiency of Add Operations
- Add at the end: O(1) time complexity
- Add at the beginning: O(1) time complexity (using addFirst())
- Add in the middle: O(n) time complexity (requires traversal to the specified position)
Comparison with ArrayList
| Operation | LinkedList | ArrayList |
|---|---|---|
| Add at the end | O(1) | O(1) amortized |
| Add at the beginning | O(1) | O(n) |
| Add in the middle | O(n) | O(n) |
| Random access | O(n) | O(1) |
Practical Application Examples
Creating a To-Do List
Example
LinkedList<String> todoList = new LinkedList<>();
// Add task
todoList.add("Buy groceries");
todoList.addFirst("Check emails"); // High priority task
todoList.add(1, "Call mom"); // Medium priority
System.out.println(todoList);
// Output: [Check emails, Call mom, Buy groceries]
// Add task
todoList.add("Buy groceries");
todoList.addFirst("Check emails"); // High priority task
todoList.add(1, "Call mom"); // Medium priority
System.out.println(todoList);
// Output: [Check emails, Call mom, Buy groceries]
Implementing a Simple Queue
Example
LinkedList<String> queue = new LinkedList<>();
// Enqueue operation
queue.add("First");
queue.add("Second");
queue.add("Third");
// Dequeue operation
while(!queue.isEmpty()) {
System.out.println(queue.removeFirst());
}
// Enqueue operation
queue.add("First");
queue.add("Second");
queue.add("Third");
// Dequeue operation
while(!queue.isEmpty()) {
System.out.println(queue.removeFirst());
}
Merging Two Lists
Example
LinkedList<String> list1 = new LinkedList<>(Arrays.asList("A", "B", "C"));
LinkedList<String> list2 = new LinkedList<>(Arrays.asList("D", "E", "F"));
list1.addAll(list2);
System.out.println(list1); // Output: [A, B, C, D, E, F]
LinkedList<String> list2 = new LinkedList<>(Arrays.asList("D", "E", "F"));
list1.addAll(list2);
System.out.println(list1); // Output: [A, B, C, D, E, F]
Notes
- Thread safety: LinkedList is not thread-safe and requires external synchronization in a multi-threaded environment
- Null elements: Adding null elements is allowed
- Index checking: Pay attention to boundary conditions when using the add method with an index
- Concurrent modification: Modifying the list while using an iterator will throw ConcurrentModificationException
Java LinkedList