2Brute2Best
🔥0
01

Full reversal (Linked List — Reversal Sub-pattern)

Walk once, flipping each arrow back to the node before it. The result is the list in reverse order, and the old tail is the new head.

Signals in the problem statement
  • ›

    "reverse the linked list"

  • ›

    "reverse a doubly linked list" (swap prev and next on every node)

  • ›

    "remove nodes that have a bigger node after them" (reverse, filter, reverse back)

  • ›

    numbers stored most significant first, where the work must start at the units digit

What makes it different

Reverse Linked List is the base move. Reverse a Doubly Linked List has prev pointers too, so swap both arrows on each node. Remove Nodes From Linked List reverses so a running maximum can be read from the right, then reverses back. Add Two Numbers II and Double a Number reverse the digits so the carry starts at the units digit; the difference is that the doubled number may gain a leading digit, which must become a new head.

Questions

ordered from “learn the idea” to “recognise it without help”
  1. 1
    Reverse Linked List
    Basic Anchor·LeetCode #206
    Easy
  2. 2
    Reverse a Doubly Linked List
    Basic Reinforcement·GeeksforGeeks
    Easy
  3. 3
    Remove Nodes From Linked List
    Slight Variation·LeetCode #2487
    Medium
  4. 4
    Add Two Numbers II
    Pattern Transfer·LeetCode #445
    Medium
  5. 5
    Double a Number Represented as a Linked List
    Tricky / Edge Case·LeetCode #2816
    Medium

Template

reverse-template.cpp
// Full reversal: walk once, pointing each node back to the previous one.
// Returns the new head (the old tail).
ListNode* reverseList(ListNode* head) {
    ListNode* prev = nullptr;                  // the reversed part so far (empty at first)
    ListNode* cur = head;                      // the next node to flip
    while (cur != nullptr) {
        ListNode* nxt = cur->next;             // 1. save the rest of the list BEFORE losing it
        cur->next = prev;                      // 2. flip this arrow
        prev = cur;                            // 3. the reversed part now starts at cur
        cur = nxt;                             // 4. move on to the saved node
    }
    return prev;                               // cur ran off the end, so prev is the old tail
}
Remember this · Linked List — Reversal
Trigger
Reverse whole / part / every k nodes, reorder, palindrome check, numbers stored as lists.
Think
Which piece do I reverse, and which nodes must I remember to reconnect afterwards?
Key question
Before reversing, who is the node just before the piece and who is just after?