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 from start and end alternately.
Analyze the time complexity of the following code snippet.
// Reorder linked list: L0->L1->...->Ln-1->Ln
// becomes L0->Ln->L1->Ln-1->L2->Ln-2->...
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 = nullptr, *curr = slow, *next;
while (curr) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
// Merge two halves
ListNode* first = head, *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 nodes alternately from each half.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Three main loops: one to find the middle, one to reverse the second half, and one to merge the halves.
- How many times: Each loop runs roughly n/2 times, where n is the number of nodes.
As the list size grows, each of the three loops processes about half the list, so total steps grow roughly in a straight line with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 15 steps (3 loops x ~5 steps each) |
| 100 | About 150 steps (3 loops x ~50 steps each) |
| 1000 | About 1500 steps (3 loops x ~500 steps each) |
Pattern observation: The total operations increase linearly as the list size increases.
Time Complexity: O(n)
This means the time to reorder grows in direct proportion to the number of nodes in the list.
[X] Wrong: "Reversing the second half or merging halves takes more than linear time because it looks like nested loops."
[OK] Correct: Each loop runs separately over different parts of the list, not nested inside each other, so total time adds up linearly, not multiplies.
Understanding this linear time complexity shows you can analyze multi-step linked list operations clearly, a skill useful for many real-world coding problems.
"What if we used recursion to reverse the second half instead of a loop? How would the time complexity change?"
Practice
Reorder Linked List problem?Solution
Step 1: Understand the problem statement
The problem asks to reorder the linked list so that nodes are taken alternately from the start and the end, moving towards the center.Step 2: Differentiate from other operations
This is not sorting or reversing the entire list, but a specific rearrangement pattern.Final Answer:
Rearrange the list so nodes alternate from start and end towards the center -> Option AQuick Check:
Reorder Linked List = Alternate nodes from start and end [OK]
- Confusing reorder with sorting
- Trying to reverse the whole list instead
- Ignoring the alternating pattern
Solution
Step 1: Check access specifiers and constructor
class ListNode { public: int val; ListNode* next; ListNode(int x) : val(x), next(nullptr) {} }; usesclasswithpublicmembers and a constructor initializingvalandnextproperly.Step 2: Identify issues in other options
A doesn't initializenext. B missespublic(members and constructor private by default). C has default constructor but no parameterized one.Final Answer:
class ListNode with public members and parameterized constructor -> Option CQuick Check:
Proper class with public and constructor = class ListNode { public: int val; ListNode* next; ListNode(int x) : val(x), next(nullptr) {} }; [OK]
- Missing public keyword in class
- Not initializing next pointer
- Using struct without constructor
1 -> 2 -> 3 -> 4 -> 5 -> null?Solution
Step 1: Identify the reorder pattern
The reorder pattern takes first node, then last node, then second node, then second last, and so on.Step 2: Apply pattern to given list
Start with 1, then 5, then 2, then 4, then 3 in the middle.Final Answer:
1 -> 5 -> 2 -> 4 -> 3 -> null -> Option AQuick Check:
Alternate front and back nodes = 1 -> 5 -> 2 -> 4 -> 3 -> null [OK]
- Reversing entire list instead
- Not alternating nodes correctly
- Leaving list unchanged
void reorderList(ListNode* head) {
if (!head) return;
ListNode* slow = head;
ListNode* fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
ListNode* second = slow;
ListNode* prev = nullptr;
while (second) {
ListNode* tmp = second->next;
second->next = prev;
prev = second;
second = tmp;
}
ListNode* first = head;
second = prev;
while (second) {
ListNode* tmp1 = first->next;
ListNode* tmp2 = second->next;
first->next = second;
second->next = tmp1;
first = tmp1;
second = tmp2;
}
}Solution
Step 1: Analyze the merging loop condition
The merging loop useswhile (second), which processes one extra node causing cycle or incorrect linking.Step 2: Correct loop condition
Changing it towhile (second->next)ensures all nodes from the second half are merged properly without overlap.Final Answer:
The merging loop condition should be while (second->next) not while (second) -> Option DQuick Check:
Loop condition must stop before the last overlapping node [OK]
- Using while(second) creates cycle
- Not reversing second half correctly
- Not handling empty list
10 -> 20 -> 30 -> 40 -> 50 -> 60 -> null. After applying the reorder linked list algorithm, what will be the final list?Solution
Step 1: Identify the reorder pattern
The reorder pattern takes nodes alternately from the front and back: first node, last node, second node, second last node, and so on.Step 2: Apply pattern to given list
Start with 10, then 60, then 20, then 50, then 30, then 40.Final Answer:
10 -> 60 -> 20 -> 50 -> 30 -> 40 -> null -> Option BQuick Check:
Alternate front and back nodes = 10 -> 60 -> 20 -> 50 -> 30 -> 40 -> null [OK]
- Reversing entire list instead
- Not alternating nodes properly
- Leaving list unchanged
Ready to prove it?
Pass the test and this pattern moves unknown
