Ctrl K

Add One to a Digit Chain

Medium
A non-negative integer is stored as a singly linked list digits: each node holds one digit, and the most significant digit is at the head. Add one to the number and return the head of a list that describes the result in the same form. If the number gets longer (as after 999), a new node must appear at the front.
Example 1
digits=list_node([4, 0, 7]) → [4, 0, 8]

407 + 1 = 408: only the last digit changes.

Example 2
digits=list_node([2, 9, 9]) → [3, 0, 0]

299 + 1 = 300: both nines turn into zeros and the carry bumps the 2.

Example 3
digits=list_node([9]) → [1, 0]

9 + 1 = 10: a new node appears at the front.

Constraints
  • 1 ≤ number of nodes ≤ 100
  • 0 ≤ Node.val ≤ 9
  • No leading zeros, except when the number itself is 0
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsAdding Digit Lists MediumForward Digit List Sum MediumGCD Between Neighbours MediumDouble the Digit Chain MediumValue of a Bit Chain Easy
loading Python…
Input
digits=list_node([4, 0, 7])
Expected
[4, 0, 8]

Run checks the first 3 tests in your browser. Submit checks all 72 tests on the server, hidden ones included.