Challenge - 5 Problems
Master of Merging Linked Lists
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of merging two sorted linked lists
What is the printed state of the merged linked list after merging these two sorted lists?
List 1: 1 -> 3 -> 5 -> null
List 2: 2 -> 4 -> 6 -> null
List 1: 1 -> 3 -> 5 -> null
List 2: 2 -> 4 -> 6 -> null
DSA Go
package main import "fmt" type ListNode struct { Val int Next *ListNode } func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode { dummy := &ListNode{} current := dummy for l1 != nil && l2 != nil { if l1.Val < l2.Val { current.Next = l1 l1 = l1.Next } else { current.Next = l2 l2 = l2.Next } current = current.Next } if l1 != nil { current.Next = l1 } else { current.Next = l2 } return dummy.Next } func printList(head *ListNode) { for head != nil { fmt.Printf("%d -> ", head.Val) head = head.Next } fmt.Println("null") } func main() { l1 := &ListNode{1, &ListNode{3, &ListNode{5, nil}}} l2 := &ListNode{2, &ListNode{4, &ListNode{6, nil}}} merged := mergeTwoLists(l1, l2) printList(merged) }
Attempts:
2 left
💡 Hint
Remember the merge process compares nodes one by one and links the smaller node first.
✗ Incorrect
The merge function compares the current nodes of both lists and appends the smaller one to the merged list. This results in a sorted merged list combining all nodes.
❓ Predict Output
intermediateOutput when one list is empty
What is the printed state of the merged linked list after merging these two lists?
List 1: null
List 2: 0 -> 7 -> 8 -> null
List 1: null
List 2: 0 -> 7 -> 8 -> null
DSA Go
package main import "fmt" type ListNode struct { Val int Next *ListNode } func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode { dummy := &ListNode{} current := dummy for l1 != nil && l2 != nil { if l1.Val < l2.Val { current.Next = l1 l1 = l1.Next } else { current.Next = l2 l2 = l2.Next } current = current.Next } if l1 != nil { current.Next = l1 } else { current.Next = l2 } return dummy.Next } func printList(head *ListNode) { for head != nil { fmt.Printf("%d -> ", head.Val) head = head.Next } fmt.Println("null") } func main() { var l1 *ListNode = nil l2 := &ListNode{0, &ListNode{7, &ListNode{8, nil}}} merged := mergeTwoLists(l1, l2) printList(merged) }
Attempts:
2 left
💡 Hint
If one list is empty, the merged list is just the other list.
✗ Incorrect
Since l1 is empty, the merged list is just l2 unchanged.
🔧 Debug
advancedIdentify the bug in this merge function
What error or problem will occur when running this merge function code?
DSA Go
func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
dummy := &ListNode{}
current := dummy
for l1 != nil || l2 != nil {
if l1.Val < l2.Val {
current.Next = l1
l1 = l1.Next
} else {
current.Next = l2
l2 = l2.Next
}
current = current.Next
}
return dummy.Next
}Attempts:
2 left
💡 Hint
Check the loop condition and what happens when l1 or l2 is nil.
✗ Incorrect
The loop uses || (or) so when one list is nil, accessing l1.Val or l2.Val causes a nil pointer dereference panic.
❓ Predict Output
advancedOutput after merging lists with duplicate values
What is the printed state of the merged linked list after merging these two sorted lists?
List 1: 1 -> 3 -> 5 -> 7 -> null
List 2: 3 -> 5 -> 7 -> 9 -> null
List 1: 1 -> 3 -> 5 -> 7 -> null
List 2: 3 -> 5 -> 7 -> 9 -> null
DSA Go
package main import "fmt" type ListNode struct { Val int Next *ListNode } func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode { dummy := &ListNode{} current := dummy for l1 != nil && l2 != nil { if l1.Val < l2.Val { current.Next = l1 l1 = l1.Next } else { current.Next = l2 l2 = l2.Next } current = current.Next } if l1 != nil { current.Next = l1 } else { current.Next = l2 } return dummy.Next } func printList(head *ListNode) { for head != nil { fmt.Printf("%d -> ", head.Val) head = head.Next } fmt.Println("null") } func main() { l1 := &ListNode{1, &ListNode{3, &ListNode{5, &ListNode{7, nil}}}} l2 := &ListNode{3, &ListNode{5, &ListNode{7, &ListNode{9, nil}}}} merged := mergeTwoLists(l1, l2) printList(merged) }
Attempts:
2 left
💡 Hint
Duplicates from both lists appear in the merged list in sorted order.
✗ Incorrect
The merge includes all nodes from both lists, so duplicates appear in order.
🧠 Conceptual
expertTime complexity of merging two sorted linked lists
What is the time complexity of merging two sorted linked lists of lengths m and n?
Attempts:
2 left
💡 Hint
Consider how many nodes are processed in total during the merge.
✗ Incorrect
Each node from both lists is visited exactly once, so the total time is proportional to the sum of their lengths.
