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
๐Ÿงฎ The Ancient Calculator ChallengeProblem: Double 567567Original Number: 567Traditional: Right-to-Left7ร—2=14 โ†’ digit=4, carry=16ร—2+1=13 โ†’ digit=3, carry=15ร—2+1=11 โ†’ digit=1, carry=1Final carry=1 โ†’ prependResult: 1134 โœ“๐Ÿ’ก The Key InsightInstead of processing right-to-left, we can predict carries!Rule: If next digit โ‰ฅ 5, current digit gets +1 carryOptimal: Left-to-Right with Lookahead1new head15ร—2%10+136ร—2%10+147ร—2%10Result: 1134 โœ“๐Ÿš€ Performance Benefitsโœ“ Time: O(n) - Single pass onlyโœ“ Space: O(1) - No extra data structuresโœ“ Elegant: Works for any size numberโœ“ In-place: Modifies original list
Understanding the Visualization
1
Identify the Problem
Doubling 567 should give 1134, but carries make it tricky
2
Traditional Approach
Process right-to-left like manual arithmetic (requires stack/reversal)
3
Clever Insight
Process left-to-right by predicting which digits will generate carries
4
Optimal Solution
Single pass with lookahead: if next digit โ‰ฅ 5, current gets +1 carry
Key Takeaway
๐ŸŽฏ Key Insight: By checking if the next digit โ‰ฅ 5, we can predict carries and process left-to-right in a single pass, achieving optimal O(n) time and O(1) space complexity!
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