Challenge - 5 Problems
Linked List Addition Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Adding Two Numbers in Linked List Form
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
Each node contains a single digit and the digits are stored in reverse order.
List 1: 2 -> 4 -> 3 -> null
List 2: 5 -> 6 -> 4 -> null
Each node contains a single digit and the digits are stored in reverse order.
DSA C#
public class ListNode { public int val; public ListNode next; public ListNode(int val=0, ListNode next=null) { this.val = val; this.next = next; } } public ListNode AddTwoNumbers(ListNode l1, ListNode l2) { ListNode dummyHead = new ListNode(0); ListNode p = l1, q = l2, curr = dummyHead; int carry = 0; while (p != null || q != null) { int x = (p != null) ? p.val : 0; int y = (q != null) ? q.val : 0; int sum = carry + x + y; carry = sum / 10; curr.next = new ListNode(sum % 10); curr = curr.next; if (p != null) p = p.next; if (q != null) q = q.next; } if (carry > 0) { curr.next = new ListNode(carry); } return dummyHead.next; } // Adding 342 + 465 = 807, reversed list: 7 -> 0 -> 8 -> null
Attempts:
2 left
💡 Hint
Add digits node by node, remember to carry over if sum is 10 or more.
✗ Incorrect
The two numbers are 342 and 465. Adding them gives 807. Since the digits are stored in reverse, the output list is 7 -> 0 -> 8 -> null.
❓ Predict Output
intermediateOutput When Lists Have Different Lengths
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 C#
// Adding 9999 + 1 = 10000, reversed list: 0 -> 0 -> 0 -> 0 -> 1 -> null
Attempts:
2 left
💡 Hint
Remember to continue adding even if one list ends, and handle carry properly.
✗ Incorrect
9999 + 1 = 10000. The reversed linked list is 0 -> 0 -> 0 -> 0 -> 1 -> null.
🔧 Debug
advancedIdentify the Bug in the Addition Code
What error will this code produce when adding two linked lists?
Code snippet:
Code snippet:
while (p != null && q != null) {
int sum = p.val + q.val + carry;
carry = sum / 10;
curr.next = new ListNode(sum % 10);
curr = curr.next;
p = p.next;
q = q.next;
}
// Missing code to handle remaining nodes or carryAttempts:
2 left
💡 Hint
Check what happens after the while loop ends.
✗ Incorrect
The loop only runs while both lists have nodes. If one list is longer, those nodes are ignored, so the sum is incomplete.
❓ Predict Output
advancedOutput When Result Has Extra Carry Node
What is the output linked list after adding these two numbers?
List 1: 5 -> 6 -> 4 -> null
List 2: 5 -> 4 -> 5 -> null
Digits stored in reverse order.
List 1: 5 -> 6 -> 4 -> null
List 2: 5 -> 4 -> 5 -> null
Digits stored in reverse order.
DSA C#
// Adding 465 + 545 = 1010, reversed list: 0 -> 1 -> 0 -> 1 -> null
Attempts:
2 left
💡 Hint
Don't forget to add the carry node at the end if carry > 0.
✗ Incorrect
465 + 545 = 1010. The reversed list is 0 -> 1 -> 0 -> 1 -> null.
🧠 Conceptual
expertWhy Use a Dummy Head Node in Addition of Linked Lists?
Why is a dummy head node commonly used when adding two numbers represented as linked lists?
Attempts:
2 left
💡 Hint
Think about how linked lists are built node by node.
✗ Incorrect
A dummy head node acts as a fixed starting point, so we don't need to check if the head is null or not when adding nodes. This makes the code cleaner and easier to manage.
