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 Represented by Linked Lists
What is the printed linked list after adding the two numbers represented by these linked lists?
List 1: 2 -> 4 -> 3 -> null
List 2: 5 -> 6 -> 4 -> null
The digits are stored in reverse order, each node contains a single digit.
List 1: 2 -> 4 -> 3 -> null
List 2: 5 -> 6 -> 4 -> null
The digits are stored in reverse order, each node contains a single digit.
DSA Java
class ListNode { int val; ListNode next; ListNode(int val) { this.val = val; } } public class Solution { public static ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode dummy = new ListNode(0); ListNode current = dummy; int carry = 0; while (l1 != null || l2 != null || carry != 0) { int sum = carry; if (l1 != null) { sum += l1.val; l1 = l1.next; } if (l2 != null) { sum += l2.val; l2 = l2.next; } carry = sum / 10; current.next = new ListNode(sum % 10); current = current.next; } return dummy.next; } public static void printList(ListNode node) { while (node != null) { System.out.print(node.val); if (node.next != null) System.out.print(" -> "); node = node.next; } System.out.println(" -> null"); } public static void main(String[] args) { ListNode l1 = new ListNode(2); l1.next = new ListNode(4); l1.next.next = new ListNode(3); ListNode l2 = new ListNode(5); l2.next = new ListNode(6); l2.next.next = new ListNode(4); ListNode result = addTwoNumbers(l1, l2); printList(result); } }
Attempts:
2 left
💡 Hint
Add digits node by node, remember to carry over when sum >= 10.
✗ Incorrect
Adding 342 + 465 = 807. The linked list stores digits in reverse order: 7 -> 0 -> 8 -> null.
❓ Predict Output
intermediateResult 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
List 1: 9 -> 9 -> 9 -> 9 -> null
List 2: 1 -> null
DSA Java
class ListNode { int val; ListNode next; ListNode(int val) { this.val = val; } } public class Solution { public static ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode dummy = new ListNode(0); ListNode current = dummy; int carry = 0; while (l1 != null || l2 != null || carry != 0) { int sum = carry; if (l1 != null) { sum += l1.val; l1 = l1.next; } if (l2 != null) { sum += l2.val; l2 = l2.next; } carry = sum / 10; current.next = new ListNode(sum % 10); current = current.next; } return dummy.next; } public static void printList(ListNode node) { while (node != null) { System.out.print(node.val); if (node.next != null) System.out.print(" -> "); node = node.next; } System.out.println(" -> null"); } public static void main(String[] args) { ListNode l1 = new ListNode(9); l1.next = new ListNode(9); l1.next.next = new ListNode(9); l1.next.next.next = new ListNode(9); ListNode l2 = new ListNode(1); ListNode result = addTwoNumbers(l1, l2); printList(result); } }
Attempts:
2 left
💡 Hint
Remember to add carry even after both lists end.
✗ Incorrect
Adding 9999 + 1 = 10000, stored as 0 -> 0 -> 0 -> 0 -> 1 -> null in reverse order.
🔧 Debug
advancedIdentify the Bug in Linked List Addition Code
What error will this code produce when adding two linked lists?
Code snippet:
Code snippet:
while (l1 != null && l2 != null) {
int sum = l1.val + l2.val + carry;
carry = sum / 10;
current.next = new ListNode(sum % 10);
current = current.next;
l1 = l1.next;
l2 = l2.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 Adding Two Large Numbers with Multiple Carries
What is the output linked list after adding these two numbers?
List 1: 9 -> 8 -> 7 -> 6 -> 5 -> null
List 2: 1 -> 2 -> 3 -> 4 -> 5 -> null
List 1: 9 -> 8 -> 7 -> 6 -> 5 -> null
List 2: 1 -> 2 -> 3 -> 4 -> 5 -> null
DSA Java
class ListNode { int val; ListNode next; ListNode(int val) { this.val = val; } } public class Solution { public static ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode dummy = new ListNode(0); ListNode current = dummy; int carry = 0; while (l1 != null || l2 != null || carry != 0) { int sum = carry; if (l1 != null) { sum += l1.val; l1 = l1.next; } if (l2 != null) { sum += l2.val; l2 = l2.next; } carry = sum / 10; current.next = new ListNode(sum % 10); current = current.next; } return dummy.next; } public static void printList(ListNode node) { while (node != null) { System.out.print(node.val); if (node.next != null) System.out.print(" -> "); node = node.next; } System.out.println(" -> null"); } public static void main(String[] args) { ListNode l1 = new ListNode(9); l1.next = new ListNode(8); l1.next.next = new ListNode(7); l1.next.next.next = new ListNode(6); l1.next.next.next.next = new ListNode(5); ListNode l2 = new ListNode(1); l2.next = new ListNode(2); l2.next.next = new ListNode(3); l2.next.next.next = new ListNode(4); l2.next.next.next.next = new ListNode(5); ListNode result = addTwoNumbers(l1, l2); printList(result); } }
Attempts:
2 left
💡 Hint
Add digits carefully and track carry for each step.
✗ Incorrect
Adding 56789 + 54321 = 111110, stored as 0 -> 1 -> 1 -> 1 -> 1 -> 1 -> null in reverse order.
🧠 Conceptual
expertWhy Use a Dummy Node in Adding Two Numbers as Linked Lists?
Why is a dummy node used at the start of the linked list when adding two numbers represented as linked lists?
Attempts:
2 left
💡 Hint
Think about how linked lists are built node by node.
✗ Incorrect
The dummy node acts as a fixed starting point so we don't need to check if the head is null or not when adding nodes.
