File tree 4 files changed +169
-0
lines changed
0002-Add-Two-Numbers/cpp-0002
4 files changed +169
-0
lines changed Original file line number Diff line number Diff line change
1
+ cmake_minimum_required (VERSION 3.5)
2
+ project (cpp_0002)
3
+
4
+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11" )
5
+
6
+ set (SOURCE_FILES main2.cpp)
7
+ add_executable (cpp_0002 ${SOURCE_FILES} )
Original file line number Diff line number Diff line change
1
+ // / Source : https://leetcode.com/problems/add-two-numbers/description/
2
+ // / Author : liuyubobobo
3
+ // / Time : 2018-08-09
4
+
5
+ #include < iostream>
6
+
7
+ using namespace std ;
8
+
9
+ // / Definition for singly-linked list.
10
+ struct ListNode {
11
+ int val;
12
+ ListNode *next;
13
+ ListNode (int x) : val(x), next(NULL ) {}
14
+ };
15
+
16
+
17
+ // / 时间复杂度: O(n)
18
+ // / 空间复杂度: O(n)
19
+ class Solution {
20
+ public:
21
+ ListNode* addTwoNumbers (ListNode* l1, ListNode* l2) {
22
+
23
+ ListNode *p1 = l1, *p2 = l2;
24
+ ListNode *dummyHead = new ListNode (-1 );
25
+ ListNode* cur = dummyHead;
26
+ int carried = 0 ;
27
+ while (p1 || p2 ){
28
+ int a = p1 ? p1->val : 0 ;
29
+ int b = p2 ? p2->val : 0 ;
30
+ cur->next = new ListNode ((a + b + carried) % 10 );
31
+ carried = (a + b + carried) / 10 ;
32
+
33
+ cur = cur->next ;
34
+ p1 = p1 ? p1->next : NULL ;
35
+ p2 = p2 ? p2->next : NULL ;
36
+ }
37
+
38
+ cur->next = carried ? new ListNode (1 ) : NULL ;
39
+ ListNode* ret = dummyHead->next ;
40
+ delete dummyHead;
41
+ return ret;
42
+ }
43
+ };
44
+
45
+
46
+ int main () {
47
+
48
+ return 0 ;
49
+ }
Original file line number Diff line number Diff line change
1
+ // / Source : https://leetcode.com/problems/add-two-numbers/description/
2
+ // / Author : liuyubobobo
3
+ // / Time : 2018-08-09
4
+
5
+ #include < iostream>
6
+
7
+ using namespace std ;
8
+
9
+ // / Definition for singly-linked list.
10
+ struct ListNode {
11
+ int val;
12
+ ListNode *next;
13
+ ListNode (int x) : val(x), next(NULL ) {}
14
+ };
15
+
16
+
17
+ // / Using l1 as the result list
18
+ // / Time Complexity: O(n)
19
+ // / Space Complexity: O(n)
20
+ class Solution {
21
+
22
+ public:
23
+ ListNode* addTwoNumbers (ListNode* l1, ListNode* l2) {
24
+
25
+ ListNode *p1 = l1, *p2 = l2;
26
+ ListNode* pre = NULL ;
27
+ int carried = 0 ;
28
+
29
+ while (p1 || p2){
30
+ int a = p1 ? p1->val : 0 ;
31
+ int b = p2 ? p2->val : 0 ;
32
+ if (p1)
33
+ p1->val = (a + b + carried) % 10 ;
34
+ else {
35
+ pre->next = new ListNode ((a + b + carried) % 10 );
36
+ p1 = pre->next ;
37
+ }
38
+ carried = (a + b + carried) / 10 ;
39
+
40
+ pre = p1;
41
+ p1 = p1->next ;
42
+ if (p2) p2 = p2->next ;
43
+ }
44
+
45
+ pre->next = carried ? new ListNode (1 ) : NULL ;
46
+ return l1;
47
+ }
48
+ };
49
+
50
+
51
+ int main () {
52
+
53
+ return 0 ;
54
+ }
Original file line number Diff line number Diff line change
1
+ // / Source : https://leetcode.com/problems/add-two-numbers/description/
2
+ // / Author : liuyubobobo
3
+ // / Time : 2018-08-09
4
+
5
+ #include < iostream>
6
+
7
+ using namespace std ;
8
+
9
+ // / Definition for singly-linked list.
10
+ struct ListNode {
11
+ int val;
12
+ ListNode *next;
13
+ ListNode (int x) : val(x), next(NULL ) {}
14
+ };
15
+
16
+
17
+ // / Using the longest list in l1 and l2 as the result list
18
+ // / Time Complexity: O(n)
19
+ // / Space Complexity: O(1)
20
+ class Solution {
21
+
22
+ public:
23
+ ListNode* addTwoNumbers (ListNode* l1, ListNode* l2) {
24
+
25
+ int len1 = getLen (l1), len2 = getLen (l2);
26
+ ListNode *p1 = len1 > len2 ? l1 : l2;
27
+ ListNode *p2 = len1 > len2 ? l2 : l1;
28
+
29
+ ListNode* pre = NULL ;
30
+ int carried = 0 ;
31
+ while (p1){
32
+ int a = p1->val ;
33
+ int b = p2 ? p2->val : 0 ;
34
+ p1->val = (a + b + carried) % 10 ;
35
+ carried = (a + b + carried) / 10 ;
36
+
37
+ pre = p1;
38
+ p1 = p1->next ;
39
+ p2 = p2 ? p2->next : NULL ;
40
+ }
41
+
42
+ pre->next = carried ? new ListNode (1 ) : NULL ;
43
+ return len1 > len2 ? l1 : l2;
44
+ }
45
+
46
+ private:
47
+ int getLen (ListNode* l){
48
+ int res = 0 ;
49
+ while (l)
50
+ res ++, l = l -> next;
51
+ return res;
52
+ }
53
+ };
54
+
55
+
56
+ int main () {
57
+
58
+ return 0 ;
59
+ }
You can’t perform that action at this time.
0 commit comments