FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
LeetCode-NOTES/Algorithms/2.Add-Two-Numbers/solution.cpp at master · moranzcw/LeetCode-NOTES · GitHub
moranzcw
/
LeetCode-NOTES
Public
Notifications
You must be signed in to change notification settings
Fork
77
Star
201
Code
Issues
1
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
LeetCode-NOTES
/
Algorithms
/
2.Add-Two-Numbers
/
solution.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
50 lines (45 loc) · 1.09 KB
Breadcrumbs
LeetCode-NOTES
/
Algorithms
/
2.Add-Two-Numbers
/
solution.cpp
Copy path
File metadata and controls
50 lines (45 loc) · 1.09 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
50
/*
*
* 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)
{
ListNode *resultList =
new
ListNode
(
0
);
ListNode *tempNode = resultList;
int
tempSum =
0
;
int
tempVal1;
int
tempVal2;
int
carry =
0
;
while
(l1 !=
NULL
|| l2 !=
NULL
)
{
if
(l1 !=
NULL
)
{
tempVal1 = l1->
val
;
l1 = l1->
next
;
}
else
tempVal1 =
0
;
if
(l2 !=
NULL
)
{
tempVal2 = l2->
val
;
l2 = l2->
next
;
}
else
tempVal2 =
0
;
tempSum = tempVal1 + tempVal2 + carry;
tempNode->
next
=
new
ListNode
(tempSum%
10
);
carry = tempSum/
10
;
tempNode = tempNode->
next
;
}
if
(carry ==
1
)
tempNode->
next
=
new
ListNode
(
1
);
return
resultList->
next
;
}
};
Back
|
FazBrowse Home
|
New Git URL