2Brute2Best
🔥0
02

Middle / split (Linked List — Fast & Slow Sub-pattern)

Slow moves 1, fast moves 2; when fast reaches the end, slow is in the middle. Use the middle to split the list, then often reverse the second half to compare it with the first.

Signals in the problem statement
  • ›

    "return the middle node"

  • ›

    "is the list a palindrome?" in O(1) space

  • ›

    "delete the middle node"

  • ›

    "pair node i with node n − 1 − i" (twin sum)

What makes it different

Middle of the Linked List is the template and returns the second middle. Palindrome Linked List finds the middle, reverses the second half, compares, and restores. Delete the Middle Node needs the node before the middle, so fast starts two nodes ahead (and a 1-node list becomes empty). Maximum Twin Sum is the palindrome walk with + instead of ==: reverse the second half and add the pairs.

Questions

ordered from “learn the idea” to “recognise it without help”
  1. 1
    Middle of the Linked List
    Basic Anchor·LeetCode #876
    Easy
  2. 2
    Palindrome Linked List
    Basic Reinforcement·LeetCode #234
    Easy
  3. 3
    Delete the Middle Node of a Linked List
    Slight Variation·LeetCode #2095
    Medium
  4. 4
    Maximum Twin Sum of a Linked List
    Medium·LeetCode #2130
    Medium

Template

middle-template.cpp
// Middle of a list: when fast reaches the end, slow is halfway.
// Even length → returns the SECOND middle (1→2→3→4 gives 3).
ListNode* middleNode(ListNode* head) {
    ListNode *slow = head, *fast = head;
    while (fast && fast->next) {
        slow = slow->next;
        fast = fast->next->next;
    }
    return slow;
}

// Node just BEFORE the middle (to delete it or cut the list in two).
// Needs at least 2 nodes. Fast starts 2 ahead, so slow stops one node earlier.
ListNode* beforeMiddle(ListNode* head) {
    ListNode *slow = head, *fast = head->next->next;
    while (fast && fast->next) {
        slow = slow->next;
        fast = fast->next->next;
    }
    return slow;
}
Remember this · Linked List — Fast & Slow
Trigger
Cycle detection, middle of list, "n-th from end", two lists meeting, repeating sequences.
Think
If one pointer moves faster or starts earlier, where will they meet?
Key question
What speed or gap makes the meeting point tell me the answer?