Linked List Problems
# 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 customNode (or ListNode) class.
*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).
4. Pattern 2: Cycle Detection (Fast and Slow Pointers)
*Prompt:* Given a linked list, determine if it has a cycle (if a node'snext 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.
5. Pattern 3: The Dummy Node technique
A common source of bugs is modifying thehead 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*
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 thenull 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.
Place a
LeftandRightpointer at the dummy node.
-
2.
Move the
Rightpointer exactlyNsteps forward. (Creating a gap of size N).
- 3. Now, move BOTH pointers forward 1 step at a time.
-
4.
When
Righthits the end of the list,Leftwill be pointing exactly at the node *before* the one you need to delete!
-
5.
Delete it:
Left.next = Left.next.next.
8. Common Mistakes
-
Null Pointer Exceptions: Trying to access
curr.next.nextwithout first checking ifcurr.nextisnull. Always check your bounds in thewhileloop condition.
-
Losing the List: If you execute
curr.next = prevbefore savingcurr.nextinto 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.
Trace the
reverseListcode on paper using 3 boxes (A -> B -> C -> null).
- 2. Why is the "Dummy Node" useful when removing nodes from a linked list?
11. MCQs
How does memory allocation differ between an Array and a Linked List?
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?
When reversing a Linked List, why must you use a next_temp variable?
What is the standard algorithm used to detect a "Cycle" (infinite loop) in a Linked List?
In the Cycle Detection algorithm, what condition proves that NO cycle exists?
What is the primary purpose of instantiating a "Dummy Node" (dummy.next = head) at the beginning of list manipulation algorithms?
What is the most common bug that crashes Linked List algorithms?
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?
How do you delete Node B if the list is A -> B -> C, and you currently have a pointer resting on A?
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
headof a linked list and an integerval, remove all the nodes of the linked list that haveNode.val == val, and return the new head." (LeetCode 203. Hint: Use a Dummy Node).
15. FAQs
- Q: Does Java/Python have pointers?
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 thehead might change. Above all, code defensively to avoid NullPointerExceptions.