Magicsheet logo

Double a Number Represented as a Linked List

Medium
47.7%
Updated 6/1/2025

Double a Number Represented as a Linked List

What is this problem about?

The Double a Number Represented as a Linked List coding problem involves an input where each node of a linked list contains a single digit of a non-negative integer. Your goal is to double this number and return the head of a new linked list representing the result. For example, if the list is 1 -> 8 -> 9 (representing 189), doubling it results in 378, so you return 3 -> 7 -> 8.

Why is this asked in interviews?

Companies like Microsoft and Nvidia use this problem to evaluate a candidate's proficiency with linked list interview pattern and their ability to handle "carry" logic in arithmetic. It's a variation of the classic "Add Two Numbers" problem but with a single operand. It tests whether you can handle potential overflows (e.g., doubling a number might add a new digit at the front) and whether you can optimize the traversal.

Algorithmic pattern used

This problem can be solved using Recursion or Stack-based reversal.

  1. Recursion: Traverse to the end of the list first. On the way back (backtracking), double the current node's value and add any carry from the next node.
  2. Reversal: Reverse the linked list, double the digits from head to tail while managing the carry, and then reverse the result back.
  3. Optimized One-Pass: For doubling specifically, a carry only occurs if the next node's value is 5 or greater. This allows for a clever one-pass solution without reversal.

Example explanation

Input: 9 -> 9 (99)

  1. Recursive call goes to the last 9.
  2. Double last 9: 18. Node value becomes 8, carry is 1.
  3. Backtrack to first 9. Double it: 18. Add carry 1: 19. Node value becomes 9, carry is 1.
  4. Since there's a carry left after the root, create a new node 1 at the front. Result: 1 -> 9 -> 8.

Common mistakes candidates make

  • Forgetting the leading carry: Not handling the case where doubling the head creates a new digit (e.g., 50 becomes 100).
  • Integer Conversion Overflow: Trying to convert the linked list to a long/big integer, doubling it, and converting it back. This will fail for very large numbers that exceed standard integer limits.
  • Incorrect Carry propagation: Adding the carry before multiplying the current digit by 2.

Interview preparation tip

Whenever you need to process a linked list from right to left (like in addition or multiplication), recursion is a very natural way to simulate a stack. If space is a concern, reversal is the standard O(1) space alternative.

Similar Questions