-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathAddTwoNumbers2.cpp
More file actions
73 lines (67 loc) · 1.66 KB
/
AddTwoNumbers2.cpp
File metadata and controls
73 lines (67 loc) · 1.66 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
66
67
68
69
70
71
72
73
class Solution
{
public:
ListNode* reverseLL(ListNode *head)
{
ListNode *curr = head;
ListNode *prev = NULL;
ListNode *forward = NULL;
while (curr)
{
forward = curr->next;
curr->next = prev;
prev = curr;
curr = forward;
}
return prev;
}
ListNode* insert_tail(ListNode *head, int val)
{
ListNode *ptr = new ListNode(val);
ListNode *temp = head;
if (head == NULL)
{
head = ptr;
return head;
}
while (temp->next)
{
temp = temp->next;
}
temp->next = ptr;
return head;
}
ListNode* adding(ListNode *l1, ListNode *l2)
{
ListNode *ans = NULL;
int carry = 0;
int sum = l1->val + l2->val;
while (l1 || l2 || carry)
{
int val1 = 0, val2 = 0;
if (l1)
val1 = l1->val;
if (l2)
val2 = l2->val;
sum = val1 + val2 + carry;
ans = insert_tail(ans, sum % 10);
carry = sum / 10;
if (l1)
l1 = l1->next;
if (l2)
l2 = l2->next;
}
return ans;
}
ListNode* addTwoNumbers(ListNode *l1, ListNode *l2)
{
//step 1
l1 = reverseLL(l1);
l2 = reverseLL(l2);
//step 2
ListNode *ans = adding(l1, l2);
//step 3
ans = reverseLL(ans);
return ans;
}
};