Double a Number Represented as a Linked List (LeetCode 2816): Solution and Explanation

Medium
Linked List
Math
Stack

Given a linked list holding the digits of a number, most significant first, return a list holding the digits of twice that number.

Intuition

Adding normally goes right to left because carries move left. A singly linked list only walks left to right, which is why many solutions reverse the list or push digits on a stack first.

Doubling is special. Each digit doubles to at most 18, so the carry it sends to the left is either 0 or 1, and it is 1 exactly when the digit is 5 or more.

That means a node can know its incoming carry just by peeking at node.next.val. Nothing further right matters, because a doubled digit plus a carry of 1 is at most 9 when the doubled digit is even and at most 8.

The only special case is the front: if the first digit is 5 or more the result gains a digit, so a 0 node goes in front to catch that carry.

Approach: One pass with a look ahead

  1. If head.val > 4, set head = ListNode(0, head).
  2. Walk node from head to the end.
  3. Set node.val = node.val * 2 % 10.
  4. If node.next exists and node.next.val > 4, add 1 to node.val.
  5. Return head.

Step-by-step example

head = [1,8,9]
  1. The head digit is 1, at most 4, so no new node is needed.
  2. Node 1: 1 * 2 = 2. The next digit 8 is 5 or more, so add 1: the node becomes 3.
  3. Node 8: 8 * 2 = 16, keep 6. The next digit 9 is 5 or more, so add 1: the node becomes 7.
  4. Node 9: 9 * 2 = 18, keep 8. There is no next node, so it stays 8.
  5. Return [3,7,8], which is 189 * 2 = 378.

Code: Python, Java, C++

1class Solution:
2    def doubleIt(self, head: Optional[ListNode]) -> Optional[ListNode]:
3        if head.val > 4:
4            head = ListNode(0, head)
5        node = head
6        while node:
7            node.val = node.val * 2 % 10
8            if node.next and node.next.val > 4:
9                node.val += 1
10            node = node.next
11        return head
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

Each of the n nodes is visited once and does constant work.

Space

O(1)

The digits are rewritten in place; at most one new node is created.

Other approaches

Reverse, double, reverse (time O(n), space O(1))

Reverse the list, double from the least significant digit with an ordinary carry, append a 1 if a carry is left, then reverse back. Correct but three passes and more pointer code.

Stack or recursion (time O(n), space O(n))

Push every node on a stack (or recurse to the end) and process digits on the way back, returning the carry. Simple to reason about but uses O(n) extra space and can overflow the call stack for 10,000 nodes.

Common mistakes

  • Converting the list to an integer. With up to 10,000 digits it overflows every fixed-width type.
  • Forgetting the new leading node when the first digit is 5 or more, which drops the top digit of the answer.
  • Checking the next node after it has already been doubled. With the look-ahead approach the next node is still the original digit when you peek, so compare with 4 on the original value.
  • Using >= 4 instead of > 4. A 4 doubles to 8 and sends no carry.

Frequently asked questions

Why can adding the carry never make a digit 10?

node.val * 2 % 10 is always even, so it is at most 8. Adding a carry of 1 gives at most 9.

Why does only the next digit decide the carry?

The carry out of a position is floor((2d + c) / 10) where c is 0 or 1. Since 2d + 1 is at most 19 and 2d + c reaches 10 exactly when d is 5 or more, the incoming carry from further right never changes the outcome.

What about the number 0?

The list [0] has a head of 0, so no node is added, and 0 * 2 = 0. The answer is [0].