leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

2816.cpp (676B)


0 class Solution {
1 static ListNode *rev(ListNode *list) {
2 ListNode *prev = nullptr, *next;
3 while (list) {
4 next = list->next;
5 list->next = prev;
6 prev = list;
7 list = next;
8 }
9 return prev;
10 }
12 public:
13 ListNode *doubleIt(ListNode *head) const {
14 head = rev(head);
16 ListNode *l = nullptr;
17 int carry = 0;
18 for (ListNode *p = head; p; l = p, p = p->next) {
19 const int val = p->val * 2 + carry;
20 p->val = val % 10;
21 carry = val / 10;
22 }
24 if (carry) l->next = new ListNode(carry);
26 return rev(head);
27 }
28 };