-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddTwoNumbers2.cpp
More file actions
65 lines (54 loc) · 1.55 KB
/
AddTwoNumbers2.cpp
File metadata and controls
65 lines (54 loc) · 1.55 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* Reverse(ListNode* head){
if(!head || !head->next){
return head;
}
ListNode* Curr = head;
ListNode* Prev = NULL;
ListNode* Next = NULL;
while(Curr){
Next = Curr->next;
Curr->next = Prev;
Prev = Curr;
Curr = Next;
}
return Prev;
}
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
l1 = Reverse(l1);
l2 = Reverse(l2);
ListNode* Result = new ListNode(0);
ListNode* Sol = Result;
ListNode* Prev = NULL;
int Carry = 0;
int Sum = 0;
while(l1 || l2 || Carry){
if(Result == NULL){
Result = new ListNode(0);
Prev->next = Result;
}
int First = l1?l1->val:0;
int Second = l2?l2->val:0;
Sum = First + Second + Carry;
Carry = Sum / 10;
Sum = Sum % 10;
Result->val = Sum;
Prev = Result;
Result = Result->next;
l1 = l1?l1->next:NULL;
l2 = l2?l2->next:NULL;
}
return Reverse(Sol);
}
};