Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
wwwanlingxiao
LeetCodeAnimation
Commits
781bbb57
Commit
781bbb57
authored
Dec 13, 2018
by
MisterBigbooo
Browse files
Add 002 Code
parent
1933a512
Changes
4
Hide whitespace changes
Inline
Side-by-side
0002-Add-Two-Numbers/cpp-0002/CMakeLists.txt
0 → 100755
View file @
781bbb57
cmake_minimum_required
(
VERSION 3.5
)
project
(
cpp_0002
)
set
(
CMAKE_CXX_FLAGS
"
${
CMAKE_CXX_FLAGS
}
-std=c++11"
)
set
(
SOURCE_FILES main2.cpp
)
add_executable
(
cpp_0002
${
SOURCE_FILES
}
)
\ No newline at end of file
0002-Add-Two-Numbers/cpp-0002/main.cpp
0 → 100755
View file @
781bbb57
/// 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
;
}
0002-Add-Two-Numbers/cpp-0002/main2.cpp
0 → 100755
View file @
781bbb57
/// 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
)
{}
};
/// Using l1 as the result list
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class
Solution
{
public:
ListNode
*
addTwoNumbers
(
ListNode
*
l1
,
ListNode
*
l2
)
{
ListNode
*
p1
=
l1
,
*
p2
=
l2
;
ListNode
*
pre
=
NULL
;
int
carried
=
0
;
while
(
p1
||
p2
){
int
a
=
p1
?
p1
->
val
:
0
;
int
b
=
p2
?
p2
->
val
:
0
;
if
(
p1
)
p1
->
val
=
(
a
+
b
+
carried
)
%
10
;
else
{
pre
->
next
=
new
ListNode
((
a
+
b
+
carried
)
%
10
);
p1
=
pre
->
next
;
}
carried
=
(
a
+
b
+
carried
)
/
10
;
pre
=
p1
;
p1
=
p1
->
next
;
if
(
p2
)
p2
=
p2
->
next
;
}
pre
->
next
=
carried
?
new
ListNode
(
1
)
:
NULL
;
return
l1
;
}
};
int
main
()
{
return
0
;
}
\ No newline at end of file
0002-Add-Two-Numbers/cpp-0002/main3.cpp
0 → 100755
View file @
781bbb57
/// 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
)
{}
};
/// Using the longest list in l1 and l2 as the result list
/// Time Complexity: O(n)
/// Space Complexity: O(1)
class
Solution
{
public:
ListNode
*
addTwoNumbers
(
ListNode
*
l1
,
ListNode
*
l2
)
{
int
len1
=
getLen
(
l1
),
len2
=
getLen
(
l2
);
ListNode
*
p1
=
len1
>
len2
?
l1
:
l2
;
ListNode
*
p2
=
len1
>
len2
?
l2
:
l1
;
ListNode
*
pre
=
NULL
;
int
carried
=
0
;
while
(
p1
){
int
a
=
p1
->
val
;
int
b
=
p2
?
p2
->
val
:
0
;
p1
->
val
=
(
a
+
b
+
carried
)
%
10
;
carried
=
(
a
+
b
+
carried
)
/
10
;
pre
=
p1
;
p1
=
p1
->
next
;
p2
=
p2
?
p2
->
next
:
NULL
;
}
pre
->
next
=
carried
?
new
ListNode
(
1
)
:
NULL
;
return
len1
>
len2
?
l1
:
l2
;
}
private:
int
getLen
(
ListNode
*
l
){
int
res
=
0
;
while
(
l
)
res
++
,
l
=
l
->
next
;
return
res
;
}
};
int
main
()
{
return
0
;
}
\ No newline at end of file
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment