← Learning Hub
Beginner

Linked Lists

A linked list is a chain of nodes; each node holds a value and a link to the next node. Adding or removing at the front is O(1), but reaching the k-th item is O(n).

Linked lists test your understanding of references (pointers). Reversing a list, finding the middle and detecting cycles are standard interview questions that build careful thinking.

Example
You can edit this code.

Fast and slow pointers

Move one pointer by 1 and another by 2. When the fast one reaches the end, the slow one is in the middle. The same trick detects loops.

Example
You can edit this code.

Java's LinkedList

Example
You can edit this code.
In real Java code, ArrayList is usually faster than LinkedList because of memory layout. Linked lists matter most for understanding pointers - and for interviews.
Key points
  • Nodes link to the next node.
  • Reverse with prev/current/next pointers.
  • Slow and fast pointers find middles and cycles.

📝 Test yourself

1. Adding at the front of a linked list takes...
2. How do you find the middle in one pass?
3. Fill in the LinkedList method that adds at the front:
songs.("A");