Plus One Linked List - Problem

Given a non-negative integer represented as a linked list of digits, plus one to the integer.

The digits are stored such that the most significant digit is at the head of the list.

For example, the number 123 is stored as 1 → 2 → 3.

Goal: Add 1 to this number and return the modified linked list.

Input & Output

Example 1 — Basic Addition
$ Input: head = [1,2,3]
Output: [1,2,4]
💡 Note: The number is 123, plus one gives 124, represented as 1→2→4
Example 2 — Carry Propagation
$ Input: head = [4,3,2,1]
Output: [4,3,2,2]
💡 Note: The number is 4321, plus one gives 4322, represented as 4→3→2→2
Example 3 — All Nines
$ Input: head = [9,9,9]
Output: [1,0,0,0]
💡 Note: The number is 999, plus one gives 1000, requiring a new head node: 1→0→0→0

Constraints

  • 1 ≤ Number of nodes ≤ 100
  • 0 ≤ Node.val ≤ 9
  • The number does not have leading zeros, except the number 0 itself

Visualization

Tap to expand
Plus One Linked List INPUT Linked List: 1 --> 2 --> 3 1 2 3 Represents: 123 Goal: 123 + 1 = 124 Input Parameters head = [1, 2, 3] Non-negative integer as list MSB LSB Most to Least Significant ALGORITHM STEPS 1 Recurse to End Go to last node (LSB) 2 Add One at End 3 + 1 = 4, carry = 0 3 Propagate Carry Return carry to parent 4 Handle Overflow New node if carry=1 Recursive Call Stack f(1) f(2) f(3) carry = 0 Unwind with result FINAL RESULT Modified Linked List 1 2 4 +1 Output [1, 2, 4] OK 123 + 1 = 124 Edge Case: [9,9,9] 999 + 1 = 1000 Output: [1,0,0,0] New head added! Key Insight: Recursion naturally processes nodes in reverse order during unwinding. We recurse to the end, add 1, and propagate carry back. If sum >= 10, digit becomes 0 and carry = 1 goes to parent. Time: O(n), Space: O(n) for recursion stack. Handle final carry by prepending new node. TutorialsPoint - Plus One Linked List | Recursive Carry Approach
Asked in
Google 15 Facebook 12 Amazon 8 Microsoft 6
32.0K Views
Medium Frequency
~15 min Avg. Time
890 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