-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path2816.cpp
More file actions
29 lines (25 loc) · 676 Bytes
/
Copy path2816.cpp
File metadata and controls
29 lines (25 loc) · 676 Bytes
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
class Solution {
static ListNode *rev(ListNode *list) {
ListNode *prev = nullptr, *next;
while (list) {
next = list->next;
list->next = prev;
prev = list;
list = next;
}
return prev;
}
public:
ListNode *doubleIt(ListNode *head) const {
head = rev(head);
ListNode *l = nullptr;
int carry = 0;
for (ListNode *p = head; p; l = p, p = p->next) {
const int val = p->val * 2 + carry;
p->val = val % 10;
carry = val / 10;
}
if (carry) l->next = new ListNode(carry);
return rev(head);
}
};