Challenge - 5 Problems
Reorder List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Reordering a Linked List
What is the printed state of the linked list after reordering it with the given function?
DSA Go
package main import "fmt" type ListNode struct { Val int Next *ListNode } func reorderList(head *ListNode) { if head == nil || head.Next == nil { return } // Find middle slow, fast := head, head for fast != nil && fast.Next != nil { slow = slow.Next fast = fast.Next.Next } // Reverse second half var prev *ListNode curr := slow.Next slow.Next = nil for curr != nil { nextTemp := curr.Next curr.Next = prev prev = curr curr = nextTemp } // Merge two halves first, second := head, prev for second != nil { tmp1 := first.Next tmp2 := second.Next first.Next = second second.Next = tmp1 first = tmp1 second = tmp2 } } func printList(head *ListNode) { for head != nil { fmt.Print(head.Val) if head.Next != nil { fmt.Print(" -> ") } head = head.Next } fmt.Println(" -> null") } func main() { head := &ListNode{1, &ListNode{2, &ListNode{3, &ListNode{4, &ListNode{5, nil}}}}} reorderList(head) printList(head) }
Attempts:
2 left
💡 Hint
Think about splitting the list, reversing the second half, then merging alternately.
✗ Incorrect
The reorderList function splits the list into two halves, reverses the second half, and then merges nodes alternately from the first and reversed second half. This results in the pattern: first node, last node, second node, second last node, and so on.
❓ Predict Output
intermediateResulting List After Reorder on Even Length List
Given the linked list 10 -> 20 -> 30 -> 40, what is the printed state after applying the reorderList function?
DSA Go
package main import "fmt" type ListNode struct { Val int Next *ListNode } func reorderList(head *ListNode) { if head == nil || head.Next == nil { return } slow, fast := head, head for fast != nil && fast.Next != nil { slow = slow.Next fast = fast.Next.Next } var prev *ListNode curr := slow.Next slow.Next = nil for curr != nil { nextTemp := curr.Next curr.Next = prev prev = curr curr = nextTemp } first, second := head, prev for second != nil { tmp1 := first.Next tmp2 := second.Next first.Next = second second.Next = tmp1 first = tmp1 second = tmp2 } } func printList(head *ListNode) { for head != nil { fmt.Print(head.Val) if head.Next != nil { fmt.Print(" -> ") } head = head.Next } fmt.Println(" -> null") } func main() { head := &ListNode{10, &ListNode{20, &ListNode{30, &ListNode{40, nil}}}} reorderList(head) printList(head) }
Attempts:
2 left
💡 Hint
Remember the second half is reversed before merging.
✗ Incorrect
The list is split into two halves: 10 -> 20 and 30 -> 40. The second half is reversed to 40 -> 30. Then merged alternately: 10 -> 40 -> 20 -> 30.
🔧 Debug
advancedIdentify the Error in Reorder Function
What error will this code produce when run?
DSA Go
package main import "fmt" type ListNode struct { Val int Next *ListNode } func reorderList(head *ListNode) { if head == nil || head.Next == nil { return } slow, fast := head, head for fast != nil && fast.Next != nil { slow = slow.Next fast = fast.Next.Next } var prev *ListNode curr := slow.Next slow.Next = nil for curr != nil { nextTemp := curr.Next curr.Next = prev prev = curr curr = nextTemp } first, second := head, prev for second != nil { tmp1 := first.Next tmp2 := second.Next first.Next = second second.Next = tmp1 first = tmp1 second = tmp2 } } func main() { var head *ListNode = nil reorderList(head) fmt.Println("Done") }
Attempts:
2 left
💡 Hint
Check how the function handles empty list input.
✗ Incorrect
The function checks if head is nil or head.Next is nil and returns immediately, so no panic occurs. The program prints 'Done' successfully.
❓ Predict Output
advancedOutput After Reordering Single Node List
What is the output of the following code?
DSA Go
package main import "fmt" type ListNode struct { Val int Next *ListNode } func reorderList(head *ListNode) { if head == nil || head.Next == nil { return } slow, fast := head, head for fast != nil && fast.Next != nil { slow = slow.Next fast = fast.Next.Next } var prev *ListNode curr := slow.Next slow.Next = nil for curr != nil { nextTemp := curr.Next curr.Next = prev prev = curr curr = nextTemp } first, second := head, prev for second != nil { tmp1 := first.Next tmp2 := second.Next first.Next = second second.Next = tmp1 first = tmp1 second = tmp2 } } func printList(head *ListNode) { for head != nil { fmt.Print(head.Val) if head.Next != nil { fmt.Print(" -> ") } head = head.Next } fmt.Println(" -> null") } func main() { head := &ListNode{42, nil} reorderList(head) printList(head) }
Attempts:
2 left
💡 Hint
A single node list should remain unchanged after reorder.
✗ Incorrect
The function returns immediately if the list has zero or one node, so the list remains unchanged and prints '42 -> null'.
🚀 Application
expertOrder of Operations in Reorder List Function
Which option correctly represents the order of key steps in the reorderList function?
Attempts:
2 left
💡 Hint
Think about when the list is split and when the second half is reversed.
✗ Incorrect
First, the middle is found (1). Then the list is split by setting middle's next to nil (3). Next, the second half is reversed (2). Finally, the two halves are merged (4).
