Skip to content

Linked List

Linked-list problems are about not losing references. Before you overwrite curr.next, save it. Draw boxes and arrows on paper (or say them out loud) for every pointer change.

// LeetCode's definition
public class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
// 1. Dummy node: you never need to special-case the head
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode curr = dummy;
// ... work ...
return dummy.next;
// 2. Fast & slow pointers
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
// slow is now the middle (the second middle for even length)
// 3. Reverse
ListNode prev = null, curr = head;
while (curr != null) {
ListNode next = curr.next; // save
curr.next = prev; // flip
prev = curr; // advance prev
curr = next; // advance curr
}
return prev; // the new head
Problem: Reverse Linked List (Easy) — LC 206

Approach (say out loud): “Iteratively, with three pointers. At each node I save next, point curr back to prev, then move both forward. At the end, prev is the new head.”

Java:

class Solution {
public ListNode reverseList(ListNode head) {
ListNode prev = null, curr = head;
while (curr != null) {
ListNode next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
// Recursive version (O(n) stack space)
public ListNode reverseListRecursive(ListNode head) {
if (head == null || head.next == null) return head;
ListNode newHead = reverseListRecursive(head.next);
head.next.next = head;
head.next = null;
return newHead;
}
}

Complexity: O(n) time, O(1) space iterative, O(n) space recursive

Edge cases: empty list, a single node.

Problem: Middle of the Linked List (Easy) — LC 876

Approach (say out loud): “Fast moves 2 steps, slow moves 1. When fast reaches the end, slow is in the middle.”

Java:

class Solution {
public ListNode middleNode(ListNode head) {
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
}

Complexity: O(n) time, O(1) space

Edge cases: even length returns the second middle. If you need the first middle, use while (fast.next != null && fast.next.next != null).

Problem: Linked List Cycle (Easy) — LC 141

Approach (say out loud): “Floyd’s algorithm. If there’s a cycle, the fast pointer eventually laps the slow one and they meet. If fast reaches null, there’s no cycle. A HashSet of visited nodes also works but uses O(n) space.”

Java:

public class Solution {
public boolean hasCycle(ListNode head) {
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) return true;
}
return false;
}
}

Complexity: O(n) time, O(1) space

Edge cases: empty list, a single node pointing to itself.

Follow-up (LC 142, cycle start): after they meet, reset one pointer to head and move both one step at a time. They meet again at the cycle start.

Problem: Merge Two Sorted Lists (Easy) — LC 21

Approach (say out loud): “Dummy node plus a tail pointer. I attach the smaller head each time. When one list runs out, I attach the rest of the other.”

Java:

class Solution {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
ListNode dummy = new ListNode(0), tail = dummy;
while (list1 != null && list2 != null) {
if (list1.val <= list2.val) {
tail.next = list1;
list1 = list1.next;
} else {
tail.next = list2;
list2 = list2.next;
}
tail = tail.next;
}
tail.next = (list1 != null) ? list1 : list2;
return dummy.next;
}
}

Complexity: O(n + m) time, O(1) space

Edge cases: one or both lists empty.

Problem: Remove Nth Node From End of List (Medium) — LC 19

Approach (say out loud): “Two pointers with a gap of n. I move fast n+1 steps ahead from a dummy, then move both until fast is null. Now slow sits just before the node to delete. The dummy handles deleting the head.”

Java:

class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(0, head);
ListNode fast = dummy, slow = dummy;
for (int i = 0; i <= n; i++) fast = fast.next; // gap of n+1
while (fast != null) {
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
return dummy.next;
}
}

Complexity: O(n) time, one pass, O(1) space

Edge cases: removing the head (n == length), a single-node list → null.

Problem: Reorder List (Medium) — LC 143

Approach (say out loud): “Three building blocks combined: find the middle, reverse the second half, then merge the two halves alternately.”

Java:

class Solution {
public void reorderList(ListNode head) {
if (head == null || head.next == null) return;
// 1. middle (first middle)
ListNode slow = head, fast = head;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
// 2. reverse the second half
ListNode prev = null, curr = slow.next;
slow.next = null;
while (curr != null) {
ListNode next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
// 3. merge alternately
ListNode first = head, second = prev;
while (second != null) {
ListNode n1 = first.next, n2 = second.next;
first.next = second;
second.next = n1;
first = n1;
second = n2;
}
}
}

Complexity: O(n) time, O(1) space

Edge cases: 1 or 2 nodes, odd vs even length.

Problem: LRU Cache (Medium) — LC 146 (also a common design question)

Approach (say out loud): “I need O(1) get and put. A HashMap gives O(1) lookup from key to node. A doubly linked list keeps the usage order: most recent near the head, least recent near the tail. On get or put I move the node to the front. When over capacity I remove the node before the tail. I use sentinel head/tail nodes so I never null-check.”

Java:

class LRUCache {
private static class Node {
int key, val;
Node prev, next;
Node(int key, int val) { this.key = key; this.val = val; }
}
private final int capacity;
private final Map<Integer, Node> map = new HashMap<>();
private final Node head = new Node(0, 0), tail = new Node(0, 0);
public LRUCache(int capacity) {
this.capacity = capacity;
head.next = tail;
tail.prev = head;
}
public int get(int key) {
Node node = map.get(key);
if (node == null) return -1;
remove(node);
addToFront(node);
return node.val;
}
public void put(int key, int value) {
Node node = map.get(key);
if (node != null) {
node.val = value;
remove(node);
addToFront(node);
return;
}
if (map.size() == capacity) {
Node lru = tail.prev;
remove(lru);
map.remove(lru.key); // this is why the node stores its key
}
Node fresh = new Node(key, value);
addToFront(fresh);
map.put(key, fresh);
}
private void remove(Node node) {
node.prev.next = node.next;
node.next.prev = node.prev;
}
private void addToFront(Node node) {
node.next = head.next;
node.prev = head;
head.next.prev = node;
head.next = node;
}
}

Complexity: O(1) for get and put, O(capacity) space

Edge cases: capacity 1, a put that updates an existing key (it must also become most recent), a get on a missing key.

Shortcut to mention: Java’s LinkedHashMap with accessOrder = true and an overridden removeEldestEntry does this in about 5 lines. Say you know it, but expect them to ask for the manual version.

class LRUCache extends LinkedHashMap<Integer, Integer> {
private final int capacity;
public LRUCache(int capacity) { super(capacity, 0.75f, true); this.capacity = capacity; }
public int get(int key) { return super.getOrDefault(key, -1); }
public void put(int key, int value) { super.put(key, value); }
@Override protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {
return size() > capacity;
}
}

As a design question (“Design a cache for our API”), also talk about: TTL expiry, thread safety (ConcurrentHashMap plus locks, or wrap with Collections.synchronizedMap), eviction policies (LRU vs LFU vs FIFO), and distributed caches (Redis with maxmemory-policy allkeys-lru).