-
Notifications
You must be signed in to change notification settings - Fork 28
/
Add_Two_Numbers_II.cpp
98 lines (94 loc) · 2.65 KB
/
Add_Two_Numbers_II.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
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
ListNode* reverseList(ListNode* head) {
ListNode* curr = head;
ListNode* newHead = nullptr;
while(curr) {
ListNode* next = curr->next;
curr->next = newHead;
newHead = curr;
curr = next;
}
return newHead;
}
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
l1 = reverseList(l1);
l2 = reverseList(l2);
ListNode* head = nullptr;
ListNode* curr = nullptr;
int carry = 0;
while(l1 or l2 or carry) {
int x = l1 ? l1->val : 0;
int y = l2 ? l2->val : 0;
int sum = x + y + carry;
if(!head) {
head = new ListNode(sum % 10);
curr = head;
} else {
curr->next = new ListNode(sum % 10);
curr = curr->next;
}
carry = sum / 10;
if(l1) l1 = l1->next;
if(l2) l2 = l2->next;
}
head = reverseList(head);
return head;
}
};
// Without altering/reversing list
class Solution {
void addTwoNumbers(ListNode* l1, int diff, ListNode* l2, ListNode* curr, ListNode* head, int& carry) {
if(!l1->next and !l2->next) {
int sum = l1->val + l2->val;
carry = sum / 10;
curr->val = sum % 10;
return;
}
curr->next = new ListNode(0);
if(diff > 0) {
addTwoNumbers(l1->next, diff - 1, l2, curr->next, head, carry);
}
else {
addTwoNumbers(l1->next, diff, l2->next, curr->next, head, carry);
}
int val1 = l1->val;
int val2 = diff > 0 ? 0 : l2->val;
int sum = val1 + val2 + carry;
carry = sum / 10;
curr->val = sum % 10;
}
int length(ListNode* head) {
int len = 0;
while(head) {
++len;
head = head->next;
}
return len;
}
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
int len1 = length(l1);
int len2 = length(l2);
int diff = len1 - len2;
int carry = 0;
ListNode* head = new ListNode(0);
ListNode* curr = head;
if(diff < 0) swap(l1, l2);
addTwoNumbers(l1, abs(diff), l2, curr, head, carry);
if(carry) {
ListNode* sentinel = new ListNode(carry);
sentinel->next = curr;
curr = sentinel;
}
return curr;
}
};