369. Plus One Linked List
Problem Description:
Given a non-negative integer represented as non-empty a singly linked list of digits, plus one to the integer.
You may assume the integer do not contain any leading zero, except the number 0 itself.
The digits are stored such that the most significant digit is at the head of the list.1
2
3
4
5
6Example:
Input:
1->2->3
Output:
1->2->4
Solution:
The key point is to find 9’s.
For example: 8->7->9->9
Add dummy: 0->8->7->9->9
The lastNotNine is 7.
7 + 1 = 8
9 change to 0.
We got 0->8->8->0->0
return dummy.next
For example: 9->9->9
Add dummy: 0->9->9->9
The lastNotNine is 0.
0 + 1 = 1
9 change to 0.
We got 1->0->0->0
return dummy
1 | /** |