Challenge - 5 Problems
Linked List Addition Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Adding Two Linked Lists Representing Numbers
What is the output linked list after adding the two numbers represented by these linked lists?
List 1: 2 -> 4 -> 3 -> null
List 2: 5 -> 6 -> 4 -> null
Note: Each node contains a single digit and digits are stored in reverse order.
List 1: 2 -> 4 -> 3 -> null
List 2: 5 -> 6 -> 4 -> null
Note: Each node contains a single digit and digits are stored in reverse order.
DSA Python
class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next def addTwoNumbers(l1, l2): dummy = ListNode() current = dummy carry = 0 while l1 or l2 or carry: val1 = l1.val if l1 else 0 val2 = l2.val if l2 else 0 total = val1 + val2 + carry carry = total // 10 current.next = ListNode(total % 10) current = current.next if l1: l1 = l1.next if l2: l2 = l2.next return dummy.next # Construct lists l1 = ListNode(2, ListNode(4, ListNode(3))) l2 = ListNode(5, ListNode(6, ListNode(4))) result = addTwoNumbers(l1, l2) output = [] while result: output.append(result.val) result = result.next print(output)
Attempts:
2 left
💡 Hint
Add digits node by node, remember to carry over if sum >= 10.
✗ Incorrect
Adding 342 + 465 gives 807. Since digits are reversed, output list is 7 -> 0 -> 8 -> null.
❓ Predict Output
intermediate2:00remaining
Result of Adding Unequal Length Linked Lists
What is the output linked list after adding these two numbers?
List 1: 9 -> 9 -> 9 -> 9 -> null
List 2: 1 -> null
Digits are stored in reverse order.
List 1: 9 -> 9 -> 9 -> 9 -> null
List 2: 1 -> null
Digits are stored in reverse order.
DSA Python
class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next def addTwoNumbers(l1, l2): dummy = ListNode() current = dummy carry = 0 while l1 or l2 or carry: val1 = l1.val if l1 else 0 val2 = l2.val if l2 else 0 total = val1 + val2 + carry carry = total // 10 current.next = ListNode(total % 10) current = current.next if l1: l1 = l1.next if l2: l2 = l2.next return dummy.next l1 = ListNode(9, ListNode(9, ListNode(9, ListNode(9)))) l2 = ListNode(1) result = addTwoNumbers(l1, l2) output = [] while result: output.append(result.val) result = result.next print(output)
Attempts:
2 left
💡 Hint
Remember to carry over after the last node if needed.
✗ Incorrect
9999 + 1 = 10000, so the linked list is 0 -> 0 -> 0 -> 0 -> 1 -> null.
🔧 Debug
advanced2:00remaining
Identify the Error in Linked List Addition Code
What error will this code raise when adding two linked lists?
Code snippet:
Code snippet:
def addTwoNumbers(l1, l2):
dummy = ListNode()
current = dummy
carry = 0
while l1 or l2:
val1 = l1.val if l1 else 0
val2 = l2.val if l2 else 0
total = val1 + val2 + carry
carry = total // 10
current.next = ListNode(total % 10)
current = current.next
if l1:
l1 = l1.next
if l2:
l2 = l2.next
if carry:
current.next = ListNode(carry)
return dummy.nextAttempts:
2 left
💡 Hint
Check the while loop condition and what happens when l1 or l2 is None.
✗ Incorrect
The loop condition misses the carry check, so when l1 and l2 become None but carry is non-zero, accessing l1.val or l2.val causes AttributeError.
❓ Predict Output
advanced2:00remaining
Output When Adding Lists with Zero Nodes
What is the output linked list after adding these two numbers?
List 1: 0 -> null
List 2: 0 -> null
Digits are stored in reverse order.
List 1: 0 -> null
List 2: 0 -> null
Digits are stored in reverse order.
DSA Python
class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next def addTwoNumbers(l1, l2): dummy = ListNode() current = dummy carry = 0 while l1 or l2 or carry: val1 = l1.val if l1 else 0 val2 = l2.val if l2 else 0 total = val1 + val2 + carry carry = total // 10 current.next = ListNode(total % 10) current = current.next if l1: l1 = l1.next if l2: l2 = l2.next return dummy.next l1 = ListNode(0) l2 = ListNode(0) result = addTwoNumbers(l1, l2) output = [] while result: output.append(result.val) result = result.next print(output)
Attempts:
2 left
💡 Hint
Adding zero plus zero should give zero.
✗ Incorrect
0 + 0 = 0, so the output list is a single node with 0.
❓ Predict Output
expert3:00remaining
Output of Adding Large Numbers with Multiple Carries
What is the output linked list after adding these two numbers?
List 1: 9 -> 9 -> 9 -> 9 -> 9 -> 9 -> 9 -> null
List 2: 9 -> 9 -> 9 -> 9 -> null
Digits are stored in reverse order.
List 1: 9 -> 9 -> 9 -> 9 -> 9 -> 9 -> 9 -> null
List 2: 9 -> 9 -> 9 -> 9 -> null
Digits are stored in reverse order.
DSA Python
class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next def addTwoNumbers(l1, l2): dummy = ListNode() current = dummy carry = 0 while l1 or l2 or carry: val1 = l1.val if l1 else 0 val2 = l2.val if l2 else 0 total = val1 + val2 + carry carry = total // 10 current.next = ListNode(total % 10) current = current.next if l1: l1 = l1.next if l2: l2 = l2.next return dummy.next l1 = ListNode(9, ListNode(9, ListNode(9, ListNode(9, ListNode(9, ListNode(9, ListNode(9))))))) l2 = ListNode(9, ListNode(9, ListNode(9, ListNode(9)))) result = addTwoNumbers(l1, l2) output = [] while result: output.append(result.val) result = result.next print(output)
Attempts:
2 left
💡 Hint
Add digit by digit with carry, carefully track carries beyond the shorter list.
✗ Incorrect
9999999 + 9999 = 10009998, reversed digits are 8 -> 9 -> 9 -> 9 -> 0 -> 0 -> 0 -> 1 -> null.