Skip to main content
Back to problems
#2816
Medium Algorithms

Double a number represented as a linked list

Linked List Math Stack
61.3% acceptance
Feb 25, 2026
1279
32
You are given the head of a non-empty linked list representing a non-negative integer without leading zeroes. Return the head of the linked list after doubling it.

Solution

Rust
Time O(n)
Space O(n)
LeetCode
solution.rs
impl Solution {
  pub fn double_it(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
    let mut vals = vec![];
    let mut cur = &head;
    while let Some(n) = cur { vals.push(n.val); cur = &n.next; }
    let mut carry = 0;
    for v in vals.iter_mut().rev() {
      let x = *v * 2 + carry;
      *v = x % 10;
      carry = x / 10;
    }
    let mut result = None;
    for &v in vals.iter().rev() {
      let mut n = ListNode::new(v);
      n.next = result;
      result = Some(Box::new(n));
    }
    if carry > 0 {
      let mut n = ListNode::new(carry);
      n.next = result;
      result = Some(Box::new(n));
    }
    result
  }
}