Before attempting this problem, you should be comfortable with:
When adding one to a number, the only digits that change are trailing nines (which become zeros) and the rightmost non-nine digit (which increments by one). If all digits are nines, we need an extra digit at the front.
By using a sentinel node before the head, we handle the case where a new digit is needed (like 999 becoming 1000) without special logic. We simply find the rightmost non-nine digit, increment it, and set all following digits to zero.
0 and point its next to the head.9, storing a reference to it.1.0 (these were all 9s that now carry over).1 (meaning we needed an extra digit), return the sentinel. Otherwise, return its next node.class Solution:
def plusOne(self, head: ListNode) -> ListNode:
# sentinel head
sentinel = ListNode(0)
sentinel.next = head
not_nine = sentinel
# find the rightmost not-nine digit
while head:
if head.val != 9:
not_nine = head
head = head.next
# increase this rightmost not-nine digit by 1
not_nine.val += 1
not_nine = not_nine.next
# set all the following nines to zeros
while not_nine:
not_nine.val = 0
not_nine = not_nine.next
return sentinel if sentinel.val else sentinel.nextWhere is the length of the input list
When all digits are 9 (e.g., 9 -> 9 -> 9), adding one requires a new node at the front. Without a sentinel node or explicit handling, you may return a result missing the leading 1, producing 0 -> 0 -> 0 instead of 1 -> 0 -> 0 -> 0.
Unlike arrays, singly linked lists cannot be traversed backwards. Naively incrementing from the head ignores how addition propagates carries from the least significant digit. This leads to incorrect results when carries need to cascade through multiple nodes.
The key insight is that only trailing nines become zeros, and the rightmost non-nine digit increments. Failing to track this node forces you to either reverse the list or use recursion, both adding unnecessary complexity or space overhead.