#369
Medium Algorithms Plus one linked list
Linked List Math
61.2% acceptance
Mar 31, 2026
966
48
No description available.
Solution
Rust
Time O(n)
Space O(n)
impl Solution {
pub fn plus_one(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
fn add_one(node: &mut Option<Box<ListNode>>) -> i32 {
match node {
None => 1,
Some(n) => {
let carry = add_one(&mut n.next);
let sum = n.val + carry;
n.val = sum % 10;
sum / 10
}
}
}
let mut head = head;
let carry = add_one(&mut head);
if carry > 0 {
let mut new_head = Box::new(ListNode::new(carry));
new_head.next = head;
Some(new_head)
} else {
head
}
}
}