📖 Scenario: You have a singly linked list representing a queue of people waiting in line. Sometimes, the queue needs to be reordered so that the first person is followed by the last person, then the second person, then the second last person, and so on.For example, if the queue is 1 -> 2 -> 3 -> 4 -> 5, after reordering it becomes 1 -> 5 -> 2 -> 4 -> 3.
🎯 Goal: You will build a program that reorders a singly linked list in this special pattern: first node, last node, second node, second last node, and so on.
📋 What You'll Learn
Create a singly linked list with nodes containing integer values
Find the middle of the linked list
Reverse the second half of the linked list
Merge the two halves by alternating nodes
Print the reordered linked list
💡 Why This Matters
🌍 Real World
Reordering linked lists is useful in scenarios like rearranging queues, scheduling tasks, or organizing data streams where alternating order is needed.
💼 Career
Understanding linked list manipulation is important for software engineering roles that involve data structure optimization, memory management, and algorithm design.
Progress0 / 4 steps
1
Create the linked list
Create a class called Node with attributes val and next. Then create a linked list with nodes having values 1, 2, 3, 4, and 5 linked in order. Store the head node in a variable called head.
DSA Python
Hint
Define a Node class with val and next. Then link nodes with values 1 to 5.
2
Find the middle of the linked list
Create two pointers called slow and fast starting at head. Move slow by one node and fast by two nodes in a loop until fast reaches the end. This will find the middle node. Store the middle node in slow.
DSA Python
Hint
Use two pointers: slow moves one step, fast moves two steps until fast reaches the end.
3
Reverse the second half of the linked list
Starting from the node after slow, reverse the linked list nodes until the end. Use variables prev set to None and curr set to slow.next. Iterate through the nodes, reversing the links. After reversing, set slow.next to None to split the list.
DSA Python
Hint
Reverse the nodes starting from slow.next by changing their next pointers.
4
Merge the two halves and print the reordered list
Merge the first half starting at head and the reversed second half starting at prev by alternating nodes. Use pointers first and second. After merging, print the reordered list values separated by ' -> ' and ending with ' -> null'.
DSA Python
Hint
Alternate nodes from the first half and reversed second half. Then print the list values with ' -> ' between them and end with ' -> null'.
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
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 ...
Step 2: Differentiate from other operations
This is not sorting or reversing the entire list, but a specific pattern of rearrangement.
Final Answer:
Rearrange the list so nodes alternate from start and end towards the center -> Option D
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
Step 1: Understand slow and fast pointer technique
Using slow and fast pointers starting at head, fast moves twice as fast to find middle.
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.
Final Answer:
Option B code snippet correctly splits the list into two halves -> Option B
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
Step 1: Identify the reorder pattern
Reorder means first node, last node, second node, second last node, and so on.
Step 2: Apply pattern to given list
For 1->2->3->4->5, reordered is 1->5->2->4->3.
Final Answer:
1 -> 5 -> 2 -> 4 -> 3 -> null -> Option A
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
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.
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.
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.
Final Answer:
The function does not return the head after reordering -> Option A
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
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
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.
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.
Final Answer:
1 -> 6 -> 2 -> 5 -> 3 -> 4 -> null; middle chosen as the first of the two middle nodes -> Option C
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 unknownstrongwhile earning +1 Grip toward your certificate.