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?
Given two linked lists representing numbers in reverse order, what is the resulting linked list after adding them?
DSA Rust
struct ListNode {
val: i32,
next: Option<Box<ListNode>>,
}
fn add_two_numbers(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut p = l1;
let mut q = l2;
let mut carry = 0;
let mut dummy = Box::new(ListNode { val: 0, next: None });
let mut current = &mut dummy;
while p.is_some() || q.is_some() || carry != 0 {
let sum = carry + p.as_ref().map_or(0, |node| node.val) + q.as_ref().map_or(0, |node| node.val);
carry = sum / 10;
current.next = Some(Box::new(ListNode { val: sum % 10, next: None }));
current = current.next.as_mut().unwrap();
p = p.and_then(|node| node.next);
q = q.and_then(|node| node.next);
}
dummy.next
}
// Input lists:
// l1: 2 -> 4 -> 3 -> null (represents 342)
// l2: 5 -> 6 -> 4 -> null (represents 465)
// What is the output linked list?Attempts:
2 left
💡 Hint
Add digits node by node, carry over if sum >= 10.
✗ Incorrect
Adding 342 + 465 = 807. The linked list stores digits in reverse order: 7 -> 0 -> 8 -> null.
🧠 Conceptual
intermediateWhy do we use a dummy head node in linked list addition?
In the function to add two numbers represented by linked lists, why is a dummy head node used?
Attempts:
2 left
💡 Hint
Think about how to easily add nodes to the result list.
✗ Incorrect
A dummy head node allows us to easily append nodes without checking if the list is empty, avoiding special cases for the first node.
🔧 Debug
advancedWhat error occurs in this Rust code for adding two numbers as linked lists?
Identify the error when running this Rust function to add two numbers represented as linked lists:
fn add_two_numbers(l1: Option>, l2: Option>) -> Option> {
let mut p = l1;
let mut q = l2;
let mut carry = 0;
let mut dummy = Box::new(ListNode { val: 0, next: None });
let mut current = &mut dummy;
while p.is_some() || q.is_some() || carry != 0 {
let sum = carry + p.unwrap().val + q.unwrap().val;
carry = sum / 10;
current.next = Some(Box::new(ListNode { val: sum % 10, next: None }));
current = current.next.as_mut().unwrap();
p = p.unwrap().next;
q = q.unwrap().next;
}
dummy.next
}
Attempts:
2 left
💡 Hint
Check how unwrap() is used on Option values inside the loop.
✗ Incorrect
Calling unwrap() on None causes a panic. The code does not check if p or q is Some before unwrap, leading to runtime error.
🚀 Application
advancedHow to handle different length linked lists in addition?
When adding two numbers represented by linked lists of different lengths, how should the code handle the shorter list?
Attempts:
2 left
💡 Hint
Think about what value to add when one list has no node.
✗ Incorrect
Missing nodes are treated as 0 so addition continues correctly until both lists and carry are processed.
❓ Predict Output
expertWhat is the output linked list after adding these two numbers?
Given the following input linked lists representing numbers in reverse order, what is the output linked list after addition?
l1: 9 -> 9 -> 9 -> 9 -> 9 -> 9 -> 9 -> null (represents 9,999,999)
l2: 9 -> 9 -> 9 -> 9 -> null (represents 9,999)
Use the standard addition method with carry.
DSA Rust
// Function and ListNode struct as usual // Input lists: // l1: 9 -> 9 -> 9 -> 9 -> 9 -> 9 -> 9 -> null // l2: 9 -> 9 -> 9 -> 9 -> null // What is the output linked list?
Attempts:
2 left
💡 Hint
Add digit by digit with carry, note the extra node for carry at the end.
✗ Incorrect
Adding 9,999,999 + 9,999 = 10,009,998. The linked list in reverse is 8 -> 9 -> 9 -> 9 -> 0 -> 0 -> 0 -> 1 -> null.
