|
| 1 | +/** |
| 2 | + * Definition for singly-linked list. |
| 3 | + * struct ListNode { |
| 4 | + * int val; |
| 5 | + * ListNode *next; |
| 6 | + * ListNode() : val(0), next(nullptr) {} |
| 7 | + * ListNode(int x) : val(x), next(nullptr) {} |
| 8 | + * ListNode(int x, ListNode *next) : val(x), next(next) {} |
| 9 | + * }; |
| 10 | + */ |
| 11 | +class Solution { |
| 12 | +public: |
| 13 | + ListNode* reverse(ListNode* head) { |
| 14 | + ListNode* curr_node = head; |
| 15 | + ListNode* next_node = curr_node->next; |
| 16 | + curr_node->next = nullptr; |
| 17 | + |
| 18 | + while (next_node != nullptr) { |
| 19 | + ListNode* next_next_node = next_node->next; |
| 20 | + next_node->next = curr_node; |
| 21 | + |
| 22 | + curr_node = next_node; |
| 23 | + next_node = next_next_node; |
| 24 | + } |
| 25 | + |
| 26 | + return curr_node; |
| 27 | + } |
| 28 | + |
| 29 | + ListNode* lesgoo(ListNode* head) { |
| 30 | + ListNode* new_head = new ListNode(-1); |
| 31 | + new_head->next = head; |
| 32 | + ListNode* curr_node = new_head; |
| 33 | + int carry = 0; |
| 34 | + |
| 35 | + while (curr_node->next != nullptr) { |
| 36 | + curr_node->next->val += curr_node->next->val; |
| 37 | + curr_node->next->val += carry; |
| 38 | + carry = curr_node->next->val / 10; |
| 39 | + curr_node->next->val %= 10; |
| 40 | + |
| 41 | + curr_node = curr_node->next; |
| 42 | + } |
| 43 | + |
| 44 | + while (carry > 0) { |
| 45 | + curr_node->next = new ListNode(carry % 10); |
| 46 | + carry /= 10; |
| 47 | + curr_node = curr_node->next; |
| 48 | + } |
| 49 | + |
| 50 | + return new_head->next; |
| 51 | + } |
| 52 | + |
| 53 | + ListNode* doubleIt(ListNode* head) { |
| 54 | + ListNode* headdd = reverse(head); |
| 55 | + return reverse(lesgoo(headdd)); |
| 56 | + } |
| 57 | +}; |
0 commit comments