Reversal and Mutation
Safely rewire linked-list references for reversal, insertion, deletion, and in-place mutation without losing nodes.
What you will be able to do
Linked-list mutation is simple only when every reference is accounted for. Reversal is the core drill: save the next node, redirect current.next, advance prev and current. If you reverse before saving next, the rest of the list can be lost.
Prev, Current, Next
A safe reversal keeps three references. `current` is the node being rewired. `next` preserves the remainder before mutation. `prev` is the reversed prefix. Each loop grows the reversed prefix by one node.
Scenario: Reversing Message Thread Chunks
A messaging backend receives an old exported thread in oldest-first order but must display a small restored segment newest-first. If the data is already represented as nodes, reversal can avoid allocating a second list, but mutating shared nodes can surprise other readers.
| Mutation question | Why it matters | Professional answer |
|---|---|---|
| Who owns the nodes? | Shared references may observe changes. | Mutate only when ownership is clear. |
| Can the operation fail midway? | Partial rewiring can corrupt the list. | Keep operations small and test edge cases. |
| Is lookup needed first? | Reversal after finding segment still needs traversal. | Include traversal cost. |
| Would copying be safer? | Copying costs memory but preserves original. | Choose based on ownership and safety. |
Same Pattern in Java, Python, and JavaScript
Iterative reversal is the interview and production-safe baseline because it avoids recursion depth risk and makes every pointer move explicit.
class ListNode { int value; ListNode next; }
class ReverseList {
static ListNode reverse(ListNode head) {
ListNode prev = null;
ListNode current = head;
while (current != null) {
ListNode next = current.next;
current.next = prev;
prev = current;
current = next;
}
return prev;
}
}Dummy Node Pattern
Deletion and insertion often become cleaner with a dummy node before head. Instead of handling head deletion as a special case, mutate dummy.next and return dummy.next at the end.
Assessment trap
Pointer mutation bugs usually come from updating references in the wrong order or forgetting that head itself may change.
Interview signalFrequently Asked Interview Questions
These are canonical interview patterns for this topic, phrased as concrete problem statements. They avoid vague theory questions and prepare you for the exact reasoning interviewers expect: invariant, edge cases, complexity, and trade-off.
- Reverse a singly linked list iteratively.
- Remove the nth node from the end of a list.
- Swap every two adjacent nodes in a linked list.
Production practiceCompany-Style Practice Examples
These are not claimed as exact company-specific questions. They are the interview patterns repeatedly used by product companies to test whether you can move from brute force to a defensible implementation.
| Example | Brute-force approach | Stronger solution | Production notes |
|---|---|---|---|
| Reverse list | Allocate a second list by copying values. | Rewire nodes with prev/current/next. | Ownership determines whether mutation is safe. |
| Remove nth from end | Handle head deletion separately. | Use dummy node plus two pointers. | Dummy simplifies boundary cases. |
| Reverse k-group | Reverse before verifying group length. | Check group exists, then reverse exactly k links. | Avoid corrupting partial suffix. |
Solved modelInterview Question Solutions
Question 1: Reverse Linked List
Reverse a singly linked list iteratively. Use prev/current/next so the unreversed suffix is saved before current.next is changed.
class ListNode { int value; ListNode next; }
class ReverseListAnswer {
static ListNode reverse(ListNode head) {
ListNode prev = null;
ListNode current = head;
while (current != null) {
ListNode next = current.next;
current.next = prev;
prev = current;
current = next;
}
return prev;
}
}Question 2: Remove Nth Node From End
Remove the nth node from the end of a list. Use a dummy node and two pointers separated by n steps, then delete slow.next.
class RemoveNth { static ListNode remove(ListNode head, int n) { ListNode dummy = new ListNode(0); dummy.next = head; ListNode fast = dummy, slow = dummy; for (int i = 0; i < n; i++) fast = fast.next; while (fast.next != null) { fast = fast.next; slow = slow.next; } slow.next = slow.next.next; return dummy.next; } }Question 3: Swap Nodes in Pairs
Swap every two adjacent nodes in a linked list. Use a dummy node and rewire pairs locally: first, second, and the node after the pair.
class SwapPairs { static ListNode swap(ListNode head) { ListNode dummy = new ListNode(0); dummy.next = head; ListNode prev = dummy; while (prev.next != null && prev.next.next != null) { ListNode first = prev.next, second = first.next; first.next = second.next; second.next = first; prev.next = second; prev = first; } return dummy.next; } }Solved modelWorked Interview Answer
Reverse a singly linked list iteratively. Save next before rewiring current.next. Then move prev and current forward. The final prev is the new head.
class ListNode { int value; ListNode next; }
class ReverseListAnswer {
static ListNode reverse(ListNode head) {
ListNode prev = null;
ListNode current = head;
while (current != null) {
ListNode next = current.next;
current.next = prev;
prev = current;
current = next;
}
return prev;
}
}Hands-on drillTry It Yourself
Practice task
For 1 -> 2 -> 3, write prev/current/next before and after each reversal iteration. Verify the returned head is 3.
Failure patternsCommon Mistakes to Avoid
- Not saving next before overwriting current.next.
- Returning the old head after reversal.
- Dropping nodes during segment reversal.
- Forgetting dummy nodes for head deletion.
- Mutating lists that other code still expects in original order.
Execution guardrailQuick-Start Checklist
- Save next node.
- Point current.next to prev.
- Advance prev to current.
- Advance current to saved next.
- Return prev after the loop.
- Use a dummy node when head may be deleted.
Recall drillKnowledge Check
| Question | Strong answer |
|---|---|
| Why save next first? | Because current.next will be overwritten during rewiring. |
| What does prev represent? | The head of the reversed prefix. |
| Why use a dummy node? | It makes mutations before the original head behave like every other mutation. |
VocabularyKey Terms
| Term | Meaning |
|---|---|
| Rewiring | Changing references between nodes. |
| Reversed prefix | The portion already reversed during iteration. |
| Dummy node | A temporary node placed before head to simplify edge cases. |
| In-place mutation | Changing the existing nodes rather than creating a new list. |
Next practiceFurther Reading
- Practice reverse list, remove nth from end, swap pairs, and reverse k-group.
- Review ownership and mutation safety in shared data structures.
- Compare iterative and recursive reversal.