Skip to main content
LeetCode Prep
CHAPTER 08 Beginner

Linked List Problems

Updated: May 18, 2026
5 min read

# CHAPTER 8

Linked List Problems

1. Chapter Introduction

Arrays store data in contiguous, sequential memory blocks. Linked Lists, however, store data in completely scattered memory locations. To connect the data, each piece of data (a "Node") contains a literal arrow (a "Pointer") pointing to the memory address of the next Node. Linked List problems in interviews rarely test complex algorithmic theory; instead, they test your ability to cleanly manage and manipulate these pointers without losing data or creating infinite loops.

2. The Structure of a Node

In an interview, the Linked List is not a built-in object; you are given a custom Node (or ListNode) class.
java
123456
// Standard representation
class ListNode {
    int val;
    ListNode next;
    ListNode(int x) { val = x; next = null; }
}

*Crucial Rule:* The only way to traverse a linked list is to start at the head and follow the next pointers. You cannot jump directly to the 5th element. Finding the 5th element is an O(N) Time operation. However, inserting a node between two known nodes is O(1) Time (you just rewire two pointers).

3. Pattern 1: Reversing a Linked List

This is arguably the most famous pointer manipulation question (LeetCode #206). *Prompt:* Reverse a singly linked list in O(1) Space. (1 -> 2 -> 3 -> null becomes 3 -> 2 -> 1 -> null). *The Logic:* You need three pointers: prev (starts as null), curr (starts as head), and next_temp (used to temporarily save the rest of the list before you break the connection).
python
123456789101112
def reverseList(head):
    prev = None
    curr = head
    
    while curr:
        next_temp = curr.next # 1. Save the rest of the list
        curr.next = prev      # 2. REVERSE the pointer backwards
        prev = curr           # 3. Slide prev forward
        curr = next_temp      # 4. Slide curr forward
        
    return prev # prev becomes the new head of the reversed list
# Time: O(N), Space: O(1)

4. Pattern 2: Cycle Detection (Fast and Slow Pointers)

*Prompt:* Given a linked list, determine if it has a cycle (if a node's next pointer points back to a previous node, creating an infinite loop). *The Logic (Floyd's Tortoise and Hare):* Use two pointers. Slow moves 1 step, Fast moves 2 steps. If there is no cycle, Fast will eventually reach null. If there is a cycle, Fast will loop around the track and eventually "lap" (collide with) Slow.
java
12345678910111213
public boolean hasCycle(ListNode head) {
    ListNode slow = head, fast = head;
    
    while (fast != null && fast.next != null) {
        slow = slow.next;          // 1 step
        fast = fast.next.next;     // 2 steps
        
        if (slow == fast) {
            return true; // Collision! Cycle detected.
        }
    }
    return false;
}

5. Pattern 3: The Dummy Node technique

A common source of bugs is modifying the head of a linked list (e.g., when deleting the very first node or merging two lists). *The Trick:* Create a fake "Dummy Node" that points to the head. dummy -> head -> node2. Now, the actual head is treated just like any other node in the middle of the list. Return dummy.next at the end.

*Example: Merge Two Sorted Lists*

python
123456789101112131415161718
def mergeTwoLists(l1, l2):
    dummy = ListNode() # The fake starting node
    tail = dummy       # The pointer we use to build the new list
    
    while l1 and l2:
        if l1.val < l2.val:
            tail.next = l1
            l1 = l1.next
        else:
            tail.next = l2
            l2 = l2.next
        tail = tail.next
        
    # Append whatever is left over
    if l1: tail.next = l1
    if l2: tail.next = l2
        
    return dummy.next # Return the true head

6. Real-World Scenario: Find the Middle Node

*Prompt:* Find the middle node of a linked list in one pass. *Logic:* Fast and Slow pointers. Fast moves 2 steps, Slow moves 1 step. When Fast hits the null at the end of the list, Slow is guaranteed to be standing exactly on the middle node.

7. Mini Project: Remove Nth Node From End

*Prompt:* Remove the Nth node from the *end* of the list. *The Logic (Two Pointers with a Gap):*
  1. 1. Place a Left and Right pointer at the dummy node.
  1. 2. Move the Right pointer exactly N steps forward. (Creating a gap of size N).
  1. 3. Now, move BOTH pointers forward 1 step at a time.
  1. 4. When Right hits the end of the list, Left will be pointing exactly at the node *before* the one you need to delete!
  1. 5. Delete it: Left.next = Left.next.next.

8. Common Mistakes

  • Null Pointer Exceptions: Trying to access curr.next.next without first checking if curr.next is null. Always check your bounds in the while loop condition.
  • Losing the List: If you execute curr.next = prev before saving curr.next into a temporary variable, the entire rest of the linked list is lost in memory forever.

9. Best Practices

  • Draw it out: Never try to solve a Linked List pointer problem purely in your head. Draw 3 boxes with arrows on the whiteboard. When you write a line of code, erase the arrow and draw it pointing to the new box.

10. Exercises

  1. 1. Trace the reverseList code on paper using 3 boxes (A -> B -> C -> null).
  1. 2. Why is the "Dummy Node" useful when removing nodes from a linked list?

11. MCQs

Question 1

How does memory allocation differ between an Array and a Linked List?

Question 2

What is the Time Complexity of inserting a new Node directly between Node A and Node B, assuming you already have a pointer to Node A?

Question 3

When reversing a Linked List, why must you use a next_temp variable?

Question 4

What is the standard algorithm used to detect a "Cycle" (infinite loop) in a Linked List?

Question 5

In the Cycle Detection algorithm, what condition proves that NO cycle exists?

Question 6

What is the primary purpose of instantiating a "Dummy Node" (dummy.next = head) at the beginning of list manipulation algorithms?

Question 7

What is the most common bug that crashes Linked List algorithms?

Question 8

If you want to find the exact Middle Node of a linked list in one single pass (O(N) Time), what technique should you use?

Question 9

How do you delete Node B if the list is A -> B -> C, and you currently have a pointer resting on A?

Question 10

Why are Linked List questions so common in interviews if they are rarely used in real-world frontend web development?

14. Interview Questions

  • Q: "Given the head of a linked list and an integer val, remove all the nodes of the linked list that have Node.val == val, and return the new head." (LeetCode 203. Hint: Use a Dummy Node).

15. FAQs

  • Q: Does Java/Python have pointers?
A: They don't have low-level C++ pointers (you can't do pointer arithmetic). Instead, they have "References." Every object variable is essentially a reference (pointer) to an object in memory.

16. Summary

Linked Lists test your ability to trace state. Always visualize the arrows. When reversing, use a temporary variable to save the list before severing the pointer. Use Fast and Slow pointers for cycle detection and finding the middle. Use a Dummy Node to gracefully handle edge cases when the head might change. Above all, code defensively to avoid NullPointerExceptions.

17. Next Chapter Recommendation

We have explored traversing arrays and linked lists linearly (O(N)). But what if the data is already sorted? Can we search it faster than O(N)? In Chapter 9: Binary Search Techniques, we will unlock the O(log N) superpower.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·