-
Notifications
You must be signed in to change notification settings - Fork 359
/
s1.cpp
32 lines (32 loc) · 786 Bytes
/
s1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// OJ: https://leetcode.com/problems/plus-one-linked-list/
// Author: github.com/lzl124631x
// 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);
}
};