Question
Formatted question description: https://leetcode.ca/all/369.html
Given a nonnegative number represented as a singly linked list of digits, plus one to the number.
The digits are stored such that the most significant digit is at the head of the list.
Example:
Input:
1>2>3
Output:
1>2>4
@tagarray
Algorithm
Reverse
The head of the table is high digit. Now let’s add 1 operation. The difficulty of this problem is that the linked list cannot access the elements by coordinates, and can only be done by traversal.
Then we think in reverse, if the end of the chain is high, then it is much more convenient to add 1 operation, and you can directly perform calculations while traversing.
So what we can do is to reverse the linked list first, and now the end of the chain is high, we add 1 to the end of the processing operation, and then reverse the linked list back.
First non9 digit, +1, then set 0 for following
Traverse the linked list and find the first number from the right that is not 9.
If you can’t find such a number, it means that all the numbers are 9, then create a new node with a value of 0 in the header, add 1 to it, and then set all the numbers on the right to 0. for example:
For example, 1>2>3
, then the first number that is not 9 is 3. Add 1 to 3 to become 4, and there is no node on the right, so no processing is done, and 1>2> 4.
For example, 8>9>9
, find the first number that is not 9 is 8, and add 1 to become 9, and then set all the following numbers to 0 to get the result 9>0 >0.
For example, in the case of 9>9>9
, if a number other than 9 is not found, then create a new node with a value of 0 in the front, add 1 to the process to become 1, and set all the following numbers 0, get 1>0>0>0
.
Code
Java

public class Plus_One_Linked_List { public ListNode plusOne_9(ListNode head) { ListNode cur = head, right = null; while (cur != null) { if (cur.val != 9) right = cur; cur = cur.next; } if (right == null) { right = new ListNode(0); right.next = head; head = right; } ++right.val; cur = right.next; while (right != null) { cur.val = 0; cur = cur.next; } return head; } public ListNode plusOne(ListNode head) { ListNode h2 = reverse(head); ListNode p = h2; while (p != null) { if (p.val + 1 <= 9) { p.val = p.val + 1; break; } else { p.val = 0; if (p.next == null) { p.next = new ListNode(1); break; } p = p.next; } } return reverse(h2); } public ListNode reverse(ListNode head) { if (head == null  head.next == null) return head; ListNode p1 = head; ListNode p2 = p1.next; while (p2 != null) { ListNode t = p2.next; p2.next = p1; p1 = p2; p2 = t; } head.next = null; return p1; } }

// OJ: https://leetcode.com/problems/plusonelinkedlist/ // Time: O(N) // Space: O(1) class Solution { private: ListNode *reverse(ListNode *head) { ListNode dummy(0); while (head) { auto p = head; head = head>next; p>next = dummy.next; dummy.next = p; } return dummy.next; } public: ListNode* plusOne(ListNode* head) { head = reverse(head); ListNode *p = head; int carry = 1; while (carry) { p>val++; carry = p>val / 10; p>val %= 10; if (!p>next) break; p = p>next; } if (carry) p>next = new ListNode(1); return reverse(head); } };

# Definition for singlylinked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def plusOne(self, head): """ :type head: ListNode :rtype: ListNode """ def reverse(cur): pre = None while cur: tmp = cur.next cur.next = pre pre = cur cur = tmp return pre p = head = reverse(head) carry = 1 pre = None while p: val = (p.val + carry) % 10 carry = 1 if val < p.val else 0 p.val = val pre = p p = p.next if carry == 1: pre.next = ListNode(1) return reverse(head)