Challenge - 5 Problems
Linked List Addition Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after adding two linked lists representing numbers?
Given two linked lists where each node contains a single digit and the digits are stored in reverse order, what is the resulting linked list after adding them?
DSA Go
type ListNode struct { Val int Next *ListNode } func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode { dummy := &ListNode{} current := dummy carry := 0 for l1 != nil || l2 != nil || carry != 0 { sum := carry if l1 != nil { sum += l1.Val l1 = l1.Next } if l2 != nil { sum += l2.Val l2 = l2.Next } carry = sum / 10 current.Next = &ListNode{Val: sum % 10} current = current.Next } return dummy.Next } // Input lists: // l1: 2 -> 4 -> 3 -> null (represents 342) // l2: 5 -> 6 -> 4 -> null (represents 465) // Call: addTwoNumbers(l1, l2)
Attempts:
2 left
💡 Hint
Add digits node by node, carry over if sum >= 10.
✗ Incorrect
Adding 2+5=7, 4+6=10 (carry 1), 3+4+1=8. So the list is 7 -> 0 -> 8 -> null.
🧠 Conceptual
intermediateWhy do we use a dummy node in the linked list addition?
In the function that adds two numbers represented by linked lists, why is a dummy node used at the start?
Attempts:
2 left
💡 Hint
Think about how to easily add nodes to an initially empty list.
✗ Incorrect
A dummy node acts as a fixed starting point, so we can easily append nodes without checking if the list is empty.
🔧 Debug
advancedWhat error occurs in this buggy linked list addition code?
Identify the error when running this code snippet for adding two numbers represented as linked lists.
DSA Go
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
var head *ListNode
var current *ListNode
carry := 0
for l1 != nil || l2 != nil {
sum := carry
if l1 != nil {
sum += l1.Val
l1 = l1.Next
}
if l2 != nil {
sum += l2.Val
l2 = l2.Next
}
carry = sum / 10
node := &ListNode{Val: sum % 10}
if head == nil {
head = node
current = node
} else {
current.Next = node
current = current.Next
}
}
if carry > 0 {
current.Next = &ListNode{Val: carry}
}
return head
}
// Input lists:
// l1: 9 -> 9 -> null
// l2: 1 -> null
// Call: addTwoNumbers(l1, l2)Attempts:
2 left
💡 Hint
Check if the carry addition after the loop is safe.
✗ Incorrect
The code correctly handles carry after the loop by adding a new node if needed, so output is 0 -> 0 -> 1 -> null.
❓ Predict Output
advancedWhat is the output when input lists have different lengths?
Given two linked lists representing numbers in reverse order, where one list is longer, what is the output after addition?
DSA Go
l1: 1 -> 8 -> null (represents 81) l2: 0 -> 1 -> 6 -> null (represents 610) // Using the standard addTwoNumbers function
Attempts:
2 left
💡 Hint
Add digits with carry, consider missing nodes as zero.
✗ Incorrect
81 + 610 = 691, reversed is 1 -> 9 -> 6, so the correct output is 1 -> 9 -> 6 -> null.
🧠 Conceptual
expertWhat is the time complexity of adding two numbers represented as linked lists?
Consider two linked lists representing numbers in reverse order. What is the time complexity of adding them using the standard approach?
Attempts:
2 left
💡 Hint
Think about how many nodes you must visit to add all digits.
✗ Incorrect
You must traverse both lists fully, so time depends on the longer list length.
