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 gets bigger.
Specifically, how the steps grow when we rearrange nodes from start and end alternately.
Analyze the time complexity of the following code snippet.
public void ReorderList(ListNode head) {
if (head == null) return;
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
ListNode second = ReverseList(slow.next);
slow.next = null;
ListNode first = head;
while (second != null) {
ListNode temp1 = first.next;
ListNode temp2 = second.next;
first.next = second;
second.next = temp1;
first = temp1;
second = temp2;
}
}
private ListNode ReverseList(ListNode head) {
ListNode prev = null;
while (head != null) {
ListNode next = head.next;
head.next = prev;
prev = head;
head = next;
}
return prev;
}
This code reorders a linked list by first finding the middle, reversing the second half, and then merging the two halves alternately.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Traversing the list multiple times: once to find the middle, once to reverse the second half, and once to merge.
- How many times: Each traversal goes through about n/2 or fewer nodes, where n is the list length.
As the list size grows, the number of steps grows roughly in a straight line with it.
| 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 operations increase linearly as the list size increases.
Time Complexity: O(n)
This means the time to reorder grows directly in proportion to the list size.
[X] Wrong: "Reversing the second half or merging will take extra nested loops, making it slower than linear."
[OK] Correct: Each step processes nodes in a single pass without nested loops, so the total work stays proportional to n.
Understanding this linear time approach shows you can handle linked list manipulations efficiently, a key skill in many coding challenges.
"What if we used recursion to reverse the second half instead of iteration? 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 arranged by alternating from the start and the end of the list.Step 2: Differentiate from other operations
This is not sorting or reversing the whole list, but a specific pattern: first node, last node, second node, second last node, and so on.Final Answer:
Rearrange the list so nodes alternate from start and end -> Option CQuick Check:
Reorder means alternate start and end nodes [OK]
- Confusing reorder with sorting
- Thinking reorder means full reversal
- Ignoring the alternating pattern
Solution
Step 1: Check for public accessibility and constructor
public class ListNode { public int val; public ListNode next; public ListNode(int x) { val = x; next = null; } } correctly declares a public class with public fields and a constructor initializing value and next pointer.Step 2: Identify missing public keywords and struct misuse
Options A and D miss public keywords making fields inaccessible; C uses struct which is not typical for linked list nodes.Final Answer:
public class ListNode { public int val; public ListNode next; public ListNode(int x) { val = x; next = null; } } -> Option AQuick Check:
Public class with constructor and public fields [OK]
- Omitting public keyword for fields
- Using struct instead of class
- Missing constructor initialization
Solution
Step 1: Identify the reorder pattern
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 remains in the middle.Final Answer:
1 -> 5 -> 2 -> 4 -> 3 -> null -> Option AQuick Check:
Alternate front and back nodes [OK]
- Reversing entire list instead of reordering
- Not alternating nodes correctly
- Leaving list unchanged
public void ReorderList(ListNode head) {
if (head == null) return;
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
ListNode second = ReverseList(slow);
ListNode first = head;
while (second != null) {
ListNode temp1 = first.next;
ListNode temp2 = second.next;
first.next = second;
second.next = temp1;
first = temp1;
second = temp2;
}
}Solution
Step 1: Analyze slow pointer position after loop
After the loop, slow points to the middle node, which is included in the reversed second half.Step 2: Understand consequence of including middle node
Including the middle node causes the merged list to have a cycle or incorrect links because the middle node is connected twice.Final Answer:
The middle node is included in the reversed second half causing cycle -> Option BQuick Check:
Exclude middle node before reversing second half [OK]
- Not splitting list before reversing second half
- Incorrect loop conditions
- Assuming ReverseList is missing error
Solution
Step 1: Identify the reorder pattern for even length list
For even nodes, reorder alternates first and last nodes: 10, 60, 20, 50, 30, 40.Step 2: Apply the pattern to given list
Start with 10, then 60, then 20, then 50, then 30, then 40, ending with null.Final Answer:
10 -> 60 -> 20 -> 50 -> 30 -> 40 -> null -> Option DQuick Check:
Alternate front and back nodes for even length [OK]
- Reversing entire list instead of reordering
- Not handling even length correctly
- Leaving list unchanged
Ready to prove it?
Pass the test and this pattern moves unknown
