Double a Number Represented as a Linked List
Medium
Topics
Given the head of a non-empty linked list whose nodes store the digits of a non-negative integer in order (most significant digit first, no leading zeros except the number 0 itself), return the list representing double that number.
Example 1
Input: head = [1,8,9] Output: [3,7,8] Explanation: 189 * 2 = 378.
Example 2
Input: head = [9,9,9] Output: [1,9,9,8]
Constraints
- 1 <= number of nodes <= 6
- 0 <= Node.val <= 9
- No leading zeros except the number 0.
Editorial — intuition, approach, complexity and pitfalls
Intuition
Parsing the digits into an integer, doubling it, and rebuilding the list is the direct approach and is what the shown solution does. It is correct here only because the constraints keep the number small enough for a native integer — and in Python that is unconditional, since integers are arbitrary precision.
In Java or C++ this approach breaks: a list of 10^4 digits is a number with 10^4 digits, far beyond any fixed-width type. So the portable solution never materialises the value.
Two ways to avoid it. Reverse the list, walk from the least significant digit doubling with carry, and reverse back. Or notice that doubling carries at most 1, and a digit generates a carry exactly when it is 5 or more — so a single forward pass works: add 1 to a node whose successor is 5 or more, then replace each digit with (2*d) % 10. A leading carry needs one new head node.
Approach
- Walk the list accumulating
value = value * 10 + digit. - Double it.
- Rebuild a list from the decimal digits of the result.
Dry Run
1 -> 8 -> 9
parse 189
double 378
rebuild 3 -> 7 -> 8
the carry-aware forward pass on the same input:
node 1, next is 8 (>= 5) -> becomes 2*1 + 1 = 3
node 8, next is 9 (>= 5) -> becomes (2*8+1) % 10 = 7
node 9, no next -> becomes (2*9) % 10 = 8
same answer, and no integer ever formedComplexity
- Time — O(n): one pass to parse, one to rebuild.
- Space — O(n): the new list, which is the result. The parse itself is O(1) in Python but O(n) conceptually in any language that needs big integers — and that is the reason to prefer the in-place digit pass, which allocates at most one extra node.
Common Pitfalls
- Assuming a native integer can hold the value. With 10^4 digits it cannot in Java or C++, and this is the point the problem is testing.
- Forgetting the leading carry. Doubling 5 or more at the head adds a digit, so
9 -> 9becomes1 -> 9 -> 8. - In the forward pass, testing the current digit rather than the next one. The carry propagates leftward, so a node is bumped by what follows it.
- Building the result in reverse and forgetting to flip it.
Full Solution — all languages
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def doubleIt(self, head):
num = 0
cur = head
while cur:
num = num * 10 + cur.val
cur = cur.next
num *= 2
dummy = ListNode(0)
t = dummy
for ch in str(num):
t.next = ListNode(int(ch))
t = t.next
return dummy.nextimport java.util.*;
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
class Solution {
public ListNode doubleIt(ListNode head) {
long num = 0;
ListNode cur = head;
while (cur != null) {
num = num * 10 + cur.val;
cur = cur.next;
}
num *= 2;
String s = Long.toString(num);
ListNode dummy = new ListNode(0), t = dummy;
for (int i = 0; i < s.length(); i++) {
t.next = new ListNode(s.charAt(i) - '0');
t = t.next;
}
return dummy.next;
}
}#include <bits/stdc++.h>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x): val(x), next(nullptr) {
}
};
class Solution {
public: ListNode* doubleIt(ListNode* head) {
long long num = 0;
ListNode* cur = head;
while (cur) {
num = num * 10 + cur->val;
cur = cur->next;
}
num *= 2;
string s = to_string(num);
ListNode dummy(0);
ListNode* t = &dummy;
for (char ch : s) {
t->next = new ListNode(ch - '0');
t = t->next;
}
return dummy.next;
}
};/** Node shape: { val, next }. Build/return plain objects or use this ctor. */ function ListNode(val, next) {
this.val = (val === undefined ? 0 : val);
this.next = (next === undefined ? null : next);
}
/**
* @param {ListNode} head
* @return {ListNode}
*/ var doubleIt = function(head) {
let num = 0, cur = head;
while (cur) {
num = num * 10 + cur.val;
cur = cur.next;
}
num *= 2;
const s = String(num);
const dummy = new ListNode(0);
let t = dummy;
for (const ch of s) {
t.next = new ListNode(+ch);
t = t.next;
}
return dummy.next;
};