Double a Number Represented as a Linked List - Problem

Imagine you have a giant number so large that it can't fit in a regular integer variable - maybe it has hundreds of digits! This number is stored as a linked list, where each node contains a single digit, and the digits are arranged from most significant to least significant (left to right).

Your task is to double this enormous number and return the result as a linked list in the same format. The input is guaranteed to represent a non-negative integer without leading zeros.

Example: If the linked list represents 123, after doubling it should represent 246.

Think of it like using an old-fashioned calculator that can only display one digit at a time, but you need to double a number that's way too big for any calculator!

Input & Output

example_1.py — Basic Doubling
$ Input: head = [1,8,9]
Output: [3,7,8]
💡 Note: The number 189 doubled is 378. Each digit is stored in a separate node: 1→8→9 becomes 3→7→8
example_2.py — Single Digit
$ Input: head = [9]
Output: [1,8]
💡 Note: The number 9 doubled is 18. Since the result has two digits, we need two nodes: 9 becomes 1→8
example_3.py — Multiple Carries
$ Input: head = [9,9,9]
Output: [1,9,9,8]
💡 Note: The number 999 doubled is 1998. Multiple carries propagate: 9×2=18, carry 1; 9×2+1=19, carry 1; 9×2+1=19, carry 1; final carry becomes new head

Constraints

  • The number of nodes in the list is in the range [1, 104]
  • 0 ≤ Node.val ≤ 9
  • The input represents a valid number without leading zeros
  • The linked list represents a non-negative integer

Visualization

Tap to expand
Double a Number in Linked List INPUT Linked List (digits left to right) 1 8 9 NULL Represents number: 189 Operation: x 2 Input: head = [1, 8, 9] ALGORITHM STEPS 1 Reverse List Process from least digit 9 --> 8 --> 1 2 Double Each Digit Track carry forward 9x2=18 --> digit:8, carry:1 8x2+1=17 --> digit:7, carry:1 1x2+1=3 --> digit:3, carry:0 3 Handle Final Carry Add node if carry != 0 4 Reverse Back Restore original order 8 --> 7 --> 3 reverses to 3 --> 7 --> 8 FINAL RESULT Doubled Linked List 3 7 8 NULL Represents number: 378 Verification: 189 x 2 = 378 OK Output: [3, 7, 8] Time: O(n) | Space: O(1) Key Insight: Reverse the linked list to process digits from least significant to most significant. This allows proper carry propagation during doubling, then reverse back for the result. TutorialsPoint - Double a Number Represented as a Linked List | Optimal Solution
Asked in
Amazon 45 Google 32 Microsoft 28 Meta 15
28.4K Views
Medium-High Frequency
~15 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen