FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
LeetCodeAnimation/0002-Add-Two-Numbers/cpp-0002/main.cpp at master · JoyJava/LeetCodeAnimation · GitHub
JoyJava
/
LeetCodeAnimation
Public
forked from
MisterBooo/LeetCodeAnimation
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
LeetCodeAnimation
/
0002-Add-Two-Numbers
/
cpp-0002
/
main.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
49 lines (38 loc) · 1.07 KB
Breadcrumbs
LeetCodeAnimation
/
0002-Add-Two-Numbers
/
cpp-0002
/
main.cpp
Copy path
File metadata and controls
executable file
·
49 lines (38 loc) · 1.07 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
//
/ Source : https://leetcode.com/problems/add-two-numbers/description/
//
/ Author : liuyubobobo
//
/ Time : 2018-08-09
#
include
<
iostream
>
using
namespace
std
;
//
/ Definition for singly-linked list.
struct
ListNode
{
int
val;
ListNode *next;
ListNode
(
int
x) : val(x), next(
NULL
) {}
};
//
/ 时间复杂度: O(n)
//
/ 空间复杂度: O(n)
class
Solution
{
public:
ListNode*
addTwoNumbers
(ListNode* l1, ListNode* l2) {
ListNode *p1 = l1, *p2 = l2;
ListNode *dummyHead =
new
ListNode
(-
1
);
ListNode* cur = dummyHead;
int
carried =
0
;
while
(p1 || p2 ){
int
a = p1 ? p1->
val
:
0
;
int
b = p2 ? p2->
val
:
0
;
cur->
next
=
new
ListNode
((a + b + carried) %
10
);
carried = (a + b + carried) /
10
;
cur = cur->
next
;
p1 = p1 ? p1->
next
:
NULL
;
p2 = p2 ? p2->
next
:
NULL
;
}
cur->
next
= carried ?
new
ListNode
(
1
) :
NULL
;
ListNode* ret = dummyHead->
next
;
delete
dummyHead;
return
ret;
}
};
int
main
() {
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL