Java LinkedList add() Method
The LinkedList class provides several overloaded versions of the add() method for adding elements to the list.
1. boolean add(E e)
This is the most basic add method; it 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"
)
;
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
Method Characteristics:
- If the index equals the list size, it is equivalent to add(E e)
- Requires traversing to the specified position; the average time complexity is O(n)
- May throw IndexOutOfBoundsException
3. boolean addAll(Collection extends E> c)
Appends all elements of the specified collection to the end of the list, in the order returned by the iterator.
Example
LinkedList
<
String
fruits
=
new
LinkedList
<>
(
);
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 argument collection
4. boolean addAll(int index, Collection extends E> c)
Inserts all elements of 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 collection at index 1
Method Characteristics:
- Needs to traverse 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 rather than the end
Performance considerations
Efficiency of Add Operations
- Add at End: O(1) time complexity
- Add at Beginning: O(1) time complexity (using addFirst())
- Add in the Middle: O(n) time complexity (needs to traverse to the specified position)
Comparison with ArrayList
| Operation | LinkedList | ArrayList |
|---|---|---|
| Add at End | O(1) | O(1) amortized |
| Add at Beginning | O(1) | O(n) |
| Add in the Middle | O(n) | O(n) |
| random access | O(n) | O(1) |
Practical Application Examples
Create a To-Do List
Example
LinkedList
<
String
todoList
=
new
LinkedList
<>
(
);
// Add tasks
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]
Implement 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
(
)
)
;
}
Merge 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]
Notes
- Thread Safety: LinkedList is not thread-safe; external synchronization is required in a multi-threaded environment
- null Element: Allows adding null elements
- Index Checking: Pay attention to boundary conditions when using the indexed add method
- Concurrent Modification: Modifying the list while using an iterator will throw ConcurrentModificationException
Java LinkedList