Java LinkedList
In Java, a LinkedList is a doubly linked list implementation in java.util that implements List<E>, Deque<E>, Cloneable, and Serializable. Elements are stored in nodes linked to previous and next nodes. It allows null elements, provides efficient insertions and removals at the list ends and implements standard deque/queue operations (push/pop, offer/poll, addFirst()/addLast()).
Syntax
LinkedList<Type> list = new LinkedList<>();
Parameters:
Type: The data type of elements stored in the list (eg.,String,Integer).
Return value:
- A new empty
LinkedListinstance of the specified type.
Common Methods in a Linked List
Common methods of LinkedList (inherited from List/Deque or defined in the class) include those for adding, removing, and accessing elements. Here are some of the most frequently used methods with their signatures and brief descriptions:
| Method | Description | Example |
|---|---|---|
boolean add(E e) |
Appends the specified element to the end of the list (same as addLast(E)). Returns true if the list changed. |
list.add("X") |
void add(int index, E element) |
Inserts an element at the specified position, shifting subsequent elements. | list.add(0, "Y") |
void addFirst(E e) |
Inserts the element at the beginning (new head). | list.addFirst("Z") |
void addLast(E e) |
Adds element at the end of the list. | list.addLast("A") |
E get(int index) |
Returns the element at the specified position. | list.get(2) |
E getFirst() / E getLast() |
Returns the first or last element. | list.getFirst() |
E remove(int index) |
Removes and returns the element at the given position. | list.remove(1) |
E removeFirst() |
Deletes and returns the first element (head). | list.removeFirst() |
E removeLast() |
Deletes and returns the last element (tail). | list.removeLast() |
boolean contains(Object o) |
Checks if the list contains the given element. | list.contains("X") |
int size() |
Returns the size or number of elements in the list. | list.size() |
Example
This example creates a LinkedList of strings and performs various operations on it:
import java.util.LinkedList;public class LinkedListDemo {public static void main(String[] args) {LinkedList<String> list = new LinkedList<>();// Adding elements to the LinkedListlist.add("Alice"); // Append "Alice" to the endlist.add("Bob"); // Append "Bob" to the endlist.addFirst("Zara"); // Insert "Zara" at the beginninglist.addLast("Charlie");// Insert "Charlie" at the end (same as add)System.out.println("List after additions: " + list);// Accessing elementsString firstElement = list.getFirst(); // Retrieve first element ("Zara")String thirdElement = list.get(2); // Retrieve element at index 2 ("Bob")System.out.println("First element: " + firstElement);System.out.println("Element at index 2: " + thirdElement);// Removing elementslist.removeFirst(); // Removes "Zara" (first element)list.removeLast(); // Removes "Charlie" (last element)list.remove("Alice"); // Removes the first occurrence of "Alice"System.out.println("List after removals: " + list);}}
The output of this code is:
List after additions: [Zara, Alice, Bob, Charlie]First element: ZaraElement at index 2: BobList after removals: [Bob]
LinkedList vs ArrayList
| Feature | LinkedList |
ArrayList |
|---|---|---|
| Access by index | O(n) (must traverse nodes) | O(1) (direct access) |
| Insert/remove ends | O(1) | O(n) at front, O(1) at end (amortized) |
| Memory usage | Higher (extra pointers per node) | Lower (contiguous array) |
| Best use case | Queues, stacks, frequent insert/remove at ends | General-purpose, fast random access |
Rule of thumb:
- Use
ArrayListfor most cases (better performance & memory). - Use
LinkedListwhen you need frequent insertions/removals at the beginning or end.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
- Beginner Friendly.17 hours