Reorder Linked List in DSA C - Time & Space Complexity
We want to understand how the time needed to reorder a linked list changes as the list grows.
Specifically, how the steps increase when we rearrange nodes in a special order.
Analyze the time complexity of the following code snippet.
// Reorder linked list: L0->L1->...->Ln-1->Ln to L0->Ln->L1->Ln-1->...
void reorderList(ListNode* head) {
if (!head || !head->next) return;
// Find middle
ListNode *slow = head, *fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
// Reverse second half
ListNode* prev = NULL;
ListNode* curr = slow;
while (curr) {
ListNode* nextTemp = curr->next;
curr->next = prev;
prev = curr;
curr = nextTemp;
}
// Merge two halves
ListNode* first = head;
ListNode* second = prev;
while (second && second->next) {
ListNode* tmp1 = first->next;
ListNode* tmp2 = second->next;
first->next = second;
second->next = tmp1;
first = tmp1;
second = tmp2;
}
}
This code reorders a linked list by first finding the middle, reversing the second half, then merging the two halves in alternating order.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Traversing the list multiple times: once to find middle, once to reverse, once to merge.
- How many times: Each traversal goes through about n/2 to n nodes, where n is list length.
As the list size grows, each step takes longer roughly proportional to the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 30 steps (3 passes of ~10 nodes) |
| 100 | About 300 steps |
| 1000 | About 3000 steps |
Pattern observation: The total steps grow roughly in a straight line with input size.
Time Complexity: O(n)
This means the time to reorder grows directly with the number of nodes in the list.
[X] Wrong: "Reversing the second half or merging halves takes more than linear time because it looks complicated."
[OK] Correct: Each step only visits each node once or twice, so total work is still proportional to n, not more.
Understanding this helps you explain how to handle linked lists efficiently, a common skill interviewers look for.
"What if we used recursion to reverse the second half instead of a loop? How would the time complexity change?"
Practice
Reorder Linked List operation?Solution
Step 1: Understand the reorder pattern
The reorder linked list operation rearranges nodes so that the first node is followed by the last node, then the second node, then the second last node, and so on.Step 2: Differentiate from other operations
This is different from sorting, reversing, or removing duplicates because it mixes nodes from start and end alternatively.Final Answer:
To rearrange the list so nodes alternate from start and end -> Option AQuick Check:
Reorder = alternate start and end nodes [OK]
- Confusing reorder with sorting
- Thinking reorder means reversing the list
- Assuming reorder removes duplicates
Solution
Step 1: Identify the fast and slow pointer technique
To find the middle node, fast pointer moves two steps, slow pointer moves one step until fast reaches end.Step 2: Check each option for correct pointer movement
while (fast && fast->next) { slow = slow->next; fast = fast->next->next; } correctly moves fast by two and slow by one. Others have incorrect pointer movements causing errors or wrong middle.Final Answer:
while (fast && fast->next) { slow = slow->next; fast = fast->next->next; } -> Option CQuick Check:
Fast moves 2 steps, slow moves 1 step [OK]
- Swapping fast and slow pointer movements
- Not checking fast->next before fast->next->next
- Using slow pointer to move two steps
Solution
Step 1: Identify the reorder pattern on the list
The reorder pattern takes first node, last node, second node, second last node, and so on.Step 2: Apply pattern to given list
Start with 1, then 5, then 2, then 4, then 3. So reordered list is 1 -> 5 -> 2 -> 4 -> 3 -> NULL.Final Answer:
1 -> 5 -> 2 -> 4 -> 3 -> NULL -> Option DQuick Check:
Alternate front and back nodes [OK]
- Reversing the entire list instead
- Keeping original order unchanged
- Skipping nodes or mixing order
void reorderList(Node* head) {
Node *slow = head, *fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next;
}
// further code omitted
}Solution
Step 1: Analyze pointer movements in the loop
Fast pointer should move two steps to find middle, but here it moves only one step.Step 2: Identify impact of incorrect fast pointer movement
This causes slow pointer to not reach the true middle, breaking reorder logic.Final Answer:
Fast pointer moves only one step instead of two -> Option AQuick Check:
Fast must move 2 steps, not 1 [OK]
- Not moving fast pointer two steps
- Ignoring NULL checks
- Confusing slow and fast pointer roles
Solution
Step 1: Find the middle node for even length list
For 6 nodes, middle is node 3 (value 30) because slow pointer stops at exact center when fast moves two steps.Step 2: Apply reorder pattern
Reorder alternates nodes from start and end: 10, 60, 20, 50, 30, 40.Final Answer:
10 -> 60 -> 20 -> 50 -> 30 -> 40 -> NULL; middle is 30 because it's the exact center for even length -> Option BQuick Check:
Middle at center, reorder alternates front and back [OK]
- Choosing wrong middle for even length
- Mixing reorder order
- Confusing node positions
Ready to prove it?
Pass the test and this pattern moves unknown
