Add 1 to a Linked List Number

medium

You are given the head of a singly linked list where every node holds a single digit (09). Reading the nodes from head to tail spells out a non-negative integer — the head is the most significant digit.

Add 1 to that number and return the result as an integer.

For example, 4 -> 5 -> 6 represents 456, and adding one gives 457. A list like 0 -> 0 -> 1 represents 1, so the answer is 2 — since we return the number, any leading zeros simply drop away.

Hints

Adding 1 usually changes just the last digit — when does the change ripple further left?
A digit that is 9 becomes 0 and passes a carry along. Find the rightmost digit that isn't a 9.
Bump that last non-nine digit, reset every 9 after it to 0, and handle the all-nines case with a new leading node.

Common doubts

Because the list is read left to right as a written number: the first node is the leftmost, highest-place digit.
They form a small number — 001 is just 1 — so adding 1 gives 2. Since we return the number, leading zeros simply disappear.

Interview follow-ups

Same odometer logic, but skip the final read-off and return the head. For the all-nines case you return the new leading node you prepended.
Reverse both lists (or recurse to the tail), add digit by digit with a running carry, and build the result — the carry mechanics are this same idea generalized to two operands.

Fun facts

  • This is the odometer problem in disguise — the same rollover logic that turns 99,999 miles into 100,000 on a car's dial.
  • The trailing-nines trick reappears in the 'plus one' array problem and inside big-integer increment routines in real math libraries.

Asked at

AmazonMicrosoftAdobeGoogleFlipkart
Frequently Sometimes Occasionally
Example 1
Input: head = 4 -> 5 -> 6
Output: 457
4->5->6 represents 456; 456 + 1 = 457.
Example 2
Input: head = 1 -> 2 -> 3
Output: 124
1->2->3 represents 123; 123 + 1 = 124.
Example 3
Input: head = 0 -> 0 -> 1
Output: 2
0->0->1 represents 001 = 1; adding 1 gives 2. Leading zeros vanish because we return the number.
Constraints

- 1 <= number of nodes <= 9 - 0 <= value of each node <= 9 - The head node holds the most significant digit.

Solve this problem →