Bird
Raised Fist0
DSA Pythonprogramming

Reorder Linked List in DSA Python

Choose your learning style10 modes available
Mental Model
We rearrange the list so nodes alternate from the start and end towards the center.
Analogy: Imagine lining up people from two ends of a line and making them walk towards the middle, alternating one from the front, then one from the back.
1 -> 2 -> 3 -> 4 -> 5 -> null
↑
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3 -> 4 -> 5 -> null
Goal: Rearrange to 1 -> 5 -> 2 -> 4 -> 3 -> null
Step 1: Find middle node using slow and fast pointers
1 -> 2 -> 3 -> 4 -> 5 -> null
           ↑slow, ↑↑fast
Why: We need to split the list into two halves at the middle
Step 2: Split list into two halves: first half 1->2->3, second half 4->5
First half: 1 -> 2 -> 3 -> null
Second half: 4 -> 5 -> null
Why: Separating halves helps us reorder by merging front and reversed back
Step 3: Reverse second half: 4 -> 5 -> null becomes 5 -> 4 -> null
Reversed second half: 5 -> 4 -> null
Why: Reversing back half allows easy alternating merge from front and back
Step 4: Merge first half and reversed second half alternately
1 -> 5 -> 2 -> 4 -> 3 -> null
Why: Alternating nodes from front and back halves achieves the reorder
Result:
1 -> 5 -> 2 -> 4 -> 3 -> null
Annotated Code
DSA Python
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def reorderList(head: ListNode) -> None:
    if not head or not head.next:
        return

    # Find middle
    slow, fast = head, head
    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next

    # Split and reverse second half
    second = slow.next
    slow.next = None
    prev = None
    while second:
        tmp = second.next
        second.next = prev
        prev = second
        second = tmp

    # Merge two halves
    first, second = head, prev
    while second:
        tmp1, tmp2 = first.next, second.next
        first.next = second
        second.next = tmp1
        first, second = tmp1, tmp2

# Helper to print list
def printList(head: ListNode) -> None:
    curr = head
    res = []
    while curr:
        res.append(str(curr.val))
        curr = curr.next
    print(" -> ".join(res) + " -> null")

# Driver code
head = ListNode(1, ListNode(2, ListNode(3, ListNode(4, ListNode(5)))))
printList(head)
reorderList(head)
printList(head)
while fast and fast.next:
advance slow and fast pointers to find middle node
second = slow.next slow.next = None
split list into two halves by breaking link at middle
while second: tmp = second.next second.next = prev prev = second second = tmp
reverse second half of the list
while second: tmp1, tmp2 = first.next, second.next first.next = second second.next = tmp1 first, second = tmp1, tmp2
merge nodes from first and reversed second half alternately
OutputSuccess
1 -> 2 -> 3 -> 4 -> 5 -> null 1 -> 5 -> 2 -> 4 -> 3 -> null
Complexity Analysis
Time: O(n) because we traverse the list multiple times but each traversal is linear
Space: O(1) because we reorder the list in place without extra data structures
vs Alternative: Naive approach might use extra arrays to reorder, costing O(n) space; this approach is more space efficient
Edge Cases
empty list
function returns immediately without changes
DSA Python
if not head or not head.next:
    return
single node list
function returns immediately without changes
DSA Python
if not head or not head.next:
    return
list with two nodes
reordering does not change list as it is already alternating
DSA Python
while fast and fast.next:
When to Use This Pattern
When asked to reorder a linked list alternating from front and back, use the reorder list pattern by splitting, reversing second half, and merging.
Common Mistakes
Mistake: Not breaking the list into two halves before reversing second half
Fix: Set slow.next = None to split the list before reversing
Mistake: Merging without handling the end of one half causing cycles
Fix: Use careful pointer updates and stop merging when second half is exhausted
Summary
Rearranges a linked list so nodes alternate from start and end towards the center.
Use when you need to reorder a list in a specific alternating pattern without extra space.
The key is to find the middle, reverse the second half, then merge alternately.

Practice

(1/5)
1. What is the main goal of the Reorder Linked List problem?
easy
A. Remove duplicate nodes from the linked list
B. Sort the linked list in ascending order
C. Reverse the entire linked list
D. Rearrange the list so nodes alternate from start and end towards the center

Solution

  1. Step 1: Understand the problem statement

    The problem asks to reorder the list so that nodes come alternately from the start and end, like L0 -> Ln -> L1 -> Ln-1 -> L2 -> Ln-2 ...
  2. Step 2: Differentiate from other operations

    This is not sorting or reversing the entire list, but a specific pattern of rearrangement.
  3. Final Answer:

    Rearrange the list so nodes alternate from start and end towards the center -> Option D
  4. Quick Check:

    Reorder means alternate start/end nodes = A [OK]
Hint: Remember: reorder means alternate front and back nodes [OK]
Common Mistakes:
  • Confusing reorder with sorting
  • Thinking reorder means full reversal
  • Assuming duplicates are removed
2. Which of the following Python code snippets correctly splits a linked list into two halves for reordering?
easy
A. slow, fast = head, head.next while fast and fast.next: slow = slow.next fast = fast.next.next second = slow.next slow.next = null
B. slow, fast = head, head while fast and fast.next: slow = slow.next fast = fast.next.next second = slow.next slow.next = null
C. slow, fast = head.next, head while fast and fast.next: slow = slow.next fast = fast.next.next second = slow.next slow.next = null
D. slow, fast = head, head while fast.next and fast.next.next: slow = slow.next fast = fast.next.next second = slow.next slow.next = null

Solution

  1. Step 1: Understand slow and fast pointer technique

    Using slow and fast pointers starting at head, fast moves twice as fast to find middle.
  2. Step 2: Check code correctness

    slow, fast = head, head while fast and fast.next: slow = slow.next fast = fast.next.next second = slow.next slow.next = null correctly initializes both at head and uses while fast and fast.next to move pointers safely.
  3. Final Answer:

    Option B code snippet correctly splits the list into two halves -> Option B
  4. Quick Check:

    Slow and fast start at head, fast moves twice = B [OK]
Hint: Start slow and fast at head; fast moves twice as fast [OK]
Common Mistakes:
  • Starting fast at head.next causes off-by-one errors
  • Incorrect while loop conditions causing crashes
  • Not breaking the list by setting slow.next = null
3. What is the output linked list after reordering the list 1 -> 2 -> 3 -> 4 -> 5 -> null?
medium
A. 1 -> 5 -> 2 -> 4 -> 3 -> null
B. 5 -> 4 -> 3 -> 2 -> 1 -> null
C. 1 -> 2 -> 3 -> 4 -> 5 -> null
D. 1 -> 3 -> 5 -> 2 -> 4 -> null

Solution

  1. Step 1: Identify the reorder pattern

    Reorder means first node, last node, second node, second last node, and so on.
  2. Step 2: Apply pattern to given list

    For 1->2->3->4->5, reordered is 1->5->2->4->3.
  3. Final Answer:

    1 -> 5 -> 2 -> 4 -> 3 -> null -> Option A
  4. Quick Check:

    Alternate front and back nodes = C [OK]
Hint: Pick nodes alternately from front and back [OK]
Common Mistakes:
  • Reversing entire list instead of reordering
  • Keeping original order unchanged
  • Skipping nodes or mixing order
4. Given the following code snippet for reordering a linked list, what is the bug?
def reorderList(head):
    if not head or not head.next:
        return
    slow, fast = head, head
    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next
    second = slow.next
    slow.next = null
    prev = null
    while second:
        temp = second.next
        second.next = prev
        prev = second
        second = temp
    first, second = head, prev
    while second:
        temp1 = first.next
        temp2 = second.next
        first.next = second
        second.next = temp1
        first = temp1
        second = temp2
medium
A. The function does not return the head after reordering
B. The slow pointer should start at head.next to find middle correctly
C. The while loop merging two halves should check first and second, not just second
D. The code does not handle the case when the list has only one node

Solution

  1. Step 1: Analyze function return behavior

    The function modifies the list in place but does not return anything, which is acceptable if list nodes are modified directly.
  2. Step 2: Check if returning head is necessary

    Since the list is modified in place, returning head is optional; however, if used in some contexts, returning head is safer.
  3. Step 3: Identify actual bug

    All other parts correctly find middle, reverse second half, and merge. The main issue is no return statement if function expected to return head.
  4. Final Answer:

    The function does not return the head after reordering -> Option A
  5. Quick Check:

    Missing return statement = A [OK]
Hint: Check if function returns the modified list head [OK]
Common Mistakes:
  • Starting slow pointer at head.next causing wrong middle
  • Merging loop condition incorrect causing infinite loop
  • Ignoring single node edge case
5. You have a linked list 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null. After applying the reorder linked list algorithm, what will be the final list? Also, explain why the middle node is chosen as it is.
hard
A. 1 -> 6 -> 2 -> 5 -> 3 -> 4 -> null; middle chosen to split list evenly
B. 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null; no reordering done for even length
C. 1 -> 6 -> 2 -> 5 -> 3 -> 4 -> null; middle chosen as the first of the two middle nodes
D. 1 -> 5 -> 2 -> 4 -> 3 -> 6 -> null; middle chosen as the last node

Solution

  1. Step 1: Apply reorder pattern to even length list

    For 1->2->3->4->5->6, reordered list is 1->6->2->5->3->4.
  2. Step 2: Explain middle node choice

    Using slow and fast pointers, slow stops at the first middle node (3), splitting list into two halves evenly.
  3. Final Answer:

    1 -> 6 -> 2 -> 5 -> 3 -> 4 -> null; middle chosen as the first of the two middle nodes -> Option C
  4. Quick Check:

    Even length split uses first middle node = D [OK]
Hint: For even length, slow stops at first middle node [OK]
Common Mistakes:
  • Choosing second middle node as split point
  • Not reordering even length lists
  • Mixing node order in final list
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard