0
0
DSA Pythonprogramming~10 mins

Reorder Linked List in DSA Python - Execution Trace

Choose your learning style9 modes available
Concept Flow - Reorder Linked List
Start at head
Find middle node using slow & fast pointers
Split list into two halves
Reverse second half
Merge first half and reversed second half alternately
Reordered list complete
The list is split into two halves, the second half is reversed, then both halves are merged alternately to reorder the list.
Execution Sample
DSA Python
def reorderList(head):
    def reverseList(head):
        prev = None
        curr = head
        while curr:
            next_temp = curr.next
            curr.next = prev
            prev = curr
            curr = next_temp
        return prev

    slow, fast = head, head
    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next
    second = slow.next
    slow.next = None
    second = reverseList(second)
    first = head
    while second:
        tmp1 = first.next
        tmp2 = second.next
        first.next = second
        second.next = tmp1
        first = tmp1
        second = tmp2
This code reorders a linked list by splitting, reversing the second half, and merging alternately.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Initialize slow and fast at head1 -> 2 -> 3 -> 4 -> 5 -> nullslow=head(1), fast=head(1)1 -> 2 -> 3 -> 4 -> 5 -> null
2Move slow and fast to find middle1 -> 2 -> 3 -> 4 -> 5 -> nullslow=2, fast=31 -> 2 -> 3 -> 4 -> 5 -> null
3Move slow and fast again1 -> 2 -> 3 -> 4 -> 5 -> nullslow=3, fast=51 -> 2 -> 3 -> 4 -> 5 -> null
4fast.next is None, stop loop1 -> 2 -> 3 -> 4 -> 5 -> nullslow=3, fast=51 -> 2 -> 3 -> 4 -> 5 -> null
5Split list at slowFirst half: 1 -> 2 -> 3 -> null Second half: 4 -> 5 -> nullslow.next = None1 -> 2 -> 3 -> null and 4 -> 5 -> null
6Reverse second half4 -> 5 -> nullReversed second half: 5 -> 4 -> null5 -> 4 -> null
7Start merging first and reversed second halfFirst half: 1 -> 2 -> 3 -> null Second half: 5 -> 4 -> nullfirst=1, second=51 -> 2 -> 3 -> null and 5 -> 4 -> null
8Merge step 1: link 1 -> 5Partial merged: 1 -> 5first.next=5, second.next=21 -> 5 -> 2 -> 3 -> null and 4 -> null
9Merge step 2: link 2 -> 4Partial merged: 1 -> 5 -> 2 -> 4first.next=4, second.next=31 -> 5 -> 2 -> 4 -> 3 -> null
10second is None, merging doneReordered list: 1 -> 5 -> 2 -> 4 -> 3 -> nullfirst=3, second=None1 -> 5 -> 2 -> 4 -> 3 -> null
💡 Merging stops when second half pointer becomes None
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5After Step 6After Step 8After Step 9Final
slow12333333
fast13555555
first11111233
second111454nullnull
List Size55555555
Key Moments - 3 Insights
Why do we stop moving fast pointer when fast.next is None?
Because fast moves two steps at a time, stopping when fast.next is None ensures slow is at the middle node (see execution_table step 4).
Why do we set slow.next to None after finding middle?
To split the list into two halves, we break the link at slow.next (see execution_table step 5), so the first half ends properly.
How does reversing the second half help in reordering?
Reversing the second half allows us to merge nodes alternately from start and end (see execution_table step 6 and merging steps 8-10).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the position of slow pointer?
ANode 3
BNode 2
CNode 4
DNode 5
💡 Hint
Check the 'Pointer Changes' column at step 3 in execution_table.
At which step does the list get split into two halves?
AStep 4
BStep 6
CStep 5
DStep 7
💡 Hint
Look for the operation mentioning 'Split list' in execution_table.
If the second half was not reversed, how would the merged list look after step 10?
A1 -> 5 -> 2 -> 4 -> 3 -> null
B1 -> 4 -> 2 -> 5 -> 3 -> null
C1 -> 2 -> 3 -> 4 -> 5 -> null
D1 -> 3 -> 2 -> 4 -> 5 -> null
💡 Hint
Reversing second half changes order; without reversing, second half remains 4 -> 5.
Concept Snapshot
Reorder Linked List:
- Find middle with slow & fast pointers
- Split list into two halves
- Reverse second half
- Merge halves alternately
- Result: nodes reordered as first, last, second, second last, ...
Full Transcript
To reorder a linked list, we first find the middle node using two pointers moving at different speeds. Then we split the list into two halves by breaking the link at the middle. Next, we reverse the second half to prepare for merging. Finally, we merge nodes from the first half and reversed second half alternately to achieve the reordered list. This process ensures the list is rearranged as first node, last node, second node, second last node, and so on.