Challenge - 5 Problems
Reorder Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Reordering a Linked List
What is the printed state of the linked list after running the reorder function on the list 1 -> 2 -> 3 -> 4 -> 5 -> null?
DSA Python
class Node: def __init__(self, val=0, next=None): self.val = val self.next = next def print_list(head): result = [] while head: result.append(str(head.val)) head = head.next print(' -> '.join(result) + ' -> null') def reorder_list(head): if not head or not head.next: return # Find middle slow, fast = head, head while fast and fast.next: slow = slow.next fast = fast.next.next # Reverse second half prev, curr = None, slow.next slow.next = None while curr: nxt = curr.next curr.next = prev prev = curr curr = nxt # Merge two halves first, second = head, prev while second: tmp1, tmp2 = first.next, second.next first.next = second second.next = tmp1 first, second = tmp1, tmp2 head = Node(1, Node(2, Node(3, Node(4, Node(5))))) reorder_list(head) print_list(head)
Attempts:
2 left
💡 Hint
Think about splitting the list, reversing the second half, then merging.
✗ Incorrect
The reorder_list function splits the list into two halves, reverses the second half, then merges nodes alternately from the first and reversed second half. This results in the order 1 -> 5 -> 2 -> 4 -> 3 -> null.
🧠 Conceptual
intermediate1:00remaining
Understanding the Purpose of Reordering
What is the main goal of the 'Reorder Linked List' algorithm?
Attempts:
2 left
💡 Hint
Think about how the first and last nodes are placed after reordering.
✗ Incorrect
The reorder algorithm rearranges nodes so the first node is followed by the last node, then the second node, then the second last, and so on, alternating from start and end.
🔧 Debug
advanced2:00remaining
Identify the Error in Reordering Code
What error will this code produce when trying to reorder the list 1 -> 2 -> 3 -> 4 -> null?
class Node:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def reorder_list(head):
slow, fast = head, head
while fast.next and fast.next.next:
slow = slow.next
fast = fast.next.next
prev, curr = None, slow.next
slow.next = None
while curr:
nxt = curr.next
curr.next = prev
prev = curr
curr = nxt
first, second = head, prev
while second:
tmp1 = first.next
tmp2 = second.next
first.next = second
second.next = tmp1
first = tmp1
second = tmp2
head = Node(1, Node(2, Node(3, Node(4))))
reorder_list(head)
Attempts:
2 left
💡 Hint
Check the while loop condition for finding the middle.
✗ Incorrect
The condition 'while fast.next and fast.next.next' can cause fast to be None in some cases, leading to AttributeError when accessing fast.next.next.
📝 Syntax
advanced1:30remaining
Correct the Syntax Error in Reorder Function
Which option fixes the syntax error in this code snippet?
reorder_list = lambda head: (
slow, fast = head, head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
)
Attempts:
2 left
💡 Hint
Lambda functions cannot contain statements like loops.
✗ Incorrect
Lambda functions in Python can only contain expressions, not statements like loops. Changing to a def function with proper syntax fixes the error.
🚀 Application
expert1:00remaining
Number of Nodes After Reordering
Given a linked list with 7 nodes, after applying the reorder list algorithm, how many nodes will be in the final list?
Attempts:
2 left
💡 Hint
Reordering changes order but not the number of nodes.
✗ Incorrect
The reorder algorithm rearranges nodes but does not add or remove any nodes, so the count remains the same.