-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddTwoNumbersII.cpp
More file actions
152 lines (127 loc) · 2.82 KB
/
addTwoNumbersII.cpp
File metadata and controls
152 lines (127 loc) · 2.82 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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* 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
* Author: Liang Xianlong
* Date: 2018.4.11
*/
#include<iostream>
#include<stack>
using namespace::std;
/**
* Definition for singly-linked list.
*/
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2);
};
/**
* method(1.翻转链表-->该方法运行更快)-验证通过
* 时间复杂度O(n)
* 空间复杂度O(n)
*/
/*ListNode* reverseList(ListNode* list) {
ListNode* p = list;
ListNode* q = list->next;
while (q != NULL) {
ListNode* t = q->next;
q->next = p;
p = q;
q = t;
}
list->next = NULL;
return p;
}
ListNode* Solution::addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* p = reverseList(l1);
ListNode* q = reverseList(l2);
ListNode* list = new ListNode(0);
ListNode* curr = list;
int carry = 0;
while (p != NULL || q != NULL) {
int x = (p != NULL) ? p->val : 0;
int y = (q != NULL) ? q->val : 0;
int sum = x + y + carry;
if (sum >= 10) {
carry = 1;
curr->next = new ListNode(sum % 10);
curr = curr->next;
} else {
carry = 0;
curr->next = new ListNode(sum % 10);
curr = curr->next;
}
if (p != NULL) {
p = p->next;
}
if (q != NULL) {
q = q->next;
}
}
if (carry > 0) {
curr->next = new ListNode(carry);
}
return reverseList(list->next);
}*/
/**
* method(2.不允许改变链表的结构-->使用栈的特性)-验证通过
* 时间复杂度O(n)
* 空间复杂度O(n)
*/
ListNode* Solution::addTwoNumbers(ListNode* l1, ListNode* l2) {
stack<int> l1s;
stack<int> l2s;
stack<int> ls;
int carry = 0;
while (l1 != NULL) {
l1s.push(l1->val);
l1 = l1->next;
}
while (l2 != NULL) {
l2s.push(l2->val);
l2 = l2->next;
}
while (!l1s.empty() || !l2s.empty()) {
int x = (!l1s.empty()) ? l1s.top() : 0;
int y = (!l2s.empty()) ? l2s.top() : 0;
int sum = x + y + carry;
if (sum >= 10) {
carry = 1;
ls.push(sum % 10);
} else {
carry = 0;
ls.push(sum % 10);
}
if (!l1s.empty()) {
l1s.pop();
}
if (!l2s.empty()) {
l2s.pop();
}
}
if (carry > 0) {
ls.push(carry);
}
ListNode* list = new ListNode(0);
ListNode* curr = list;
while (!ls.empty()) {
curr->next = new ListNode(ls.top());
ls.pop();
curr = curr->next;
}
return list->next;
}
int main(void) {
return 0;
}