Double a Number Represented as a Linked List

Medium

Topics
Linked ListMathStack

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

before189null
after378null
Digits are stored most significant first, so the carry travels against the direction of traversal. Two ways round it: reverse, double, reverse back; or use the fact that doubling carries at most 1, so a digit only needs to look at whether the next digit is 5 or more.
Linked list values from head to tail.
Input:  head = [1,8,9]
Output: [3,7,8]
Explanation: 189 * 2 = 378.

Example 2

before999null
after1998null
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

  1. Walk the list accumulating value = value * 10 + digit.
  2. Double it.
  3. 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 formed

Complexity

  • 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 -> 9 becomes 1 -> 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.next
SensAI
Beta
Tap the mic to stop voice mode