-
Notifications
You must be signed in to change notification settings - Fork 522
/
Copy path07. Add Two Numbers II.cpp
115 lines (100 loc) · 2.41 KB
/
07. 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
Add Two Numbers II
==================
You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.
Example:
Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 8 -> 0 -> 7
*/
/**
* 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
{
ListNode *reverse(ListNode *head)
{
if (!head || !head->next)
return head;
auto prev = head, curr = head->next, next = head->next->next;
while (curr)
{
curr->next = prev;
prev = curr;
curr = next;
if (!curr)
break;
next = curr->next;
}
head->next = NULL;
return prev;
}
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2)
{
stack<int> n1, n2;
while (l1)
{
n1.push(l1->val);
l1 = l1->next;
}
while (l2)
{
n2.push(l2->val);
l2 = l2->next;
}
ListNode *dummy = new ListNode(-1);
auto temp = dummy;
int carry = 0;
while (n1.size() && n2.size())
{
int num = n1.top() + n2.top() + carry;
n1.pop();
n2.pop();
carry = num / 10;
num = num % 10;
ListNode *newNode = new ListNode(num);
temp->next = newNode;
temp = temp->next;
}
while (n1.size())
{
int num = n1.top() + carry;
n1.pop();
carry = num / 10;
num = num % 10;
ListNode *newNode = new ListNode(num);
temp->next = newNode;
temp = temp->next;
}
while (n2.size())
{
int num = n2.top() + carry;
n2.pop();
carry = num / 10;
num = num % 10;
ListNode *newNode = new ListNode(num);
temp->next = newNode;
temp = temp->next;
}
while (carry)
{
int num = carry % 10;
carry = carry / 10;
ListNode *newNode = new ListNode(num);
temp->next = newNode;
temp = temp->next;
}
temp = reverse(dummy->next);
return temp;
}
};