FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode-algorithms/javascript/AddTwoNumbers.js at master · anishLearnsToCode/leetcode-algorithms · GitHub
anishLearnsToCode
/
leetcode-algorithms
Public
Notifications
You must be signed in to change notification settings
Fork
17
Star
98
Code
Issues
0
Pull requests
0
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode-algorithms
/
javascript
/
AddTwoNumbers.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
39 lines (35 loc) · 814 Bytes
Breadcrumbs
leetcode-algorithms
/
javascript
/
AddTwoNumbers.js
Copy path
File metadata and controls
39 lines (35 loc) · 814 Bytes
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
// https://leetcode.com/problems/add-two-numbers/
// T: O(max(|l1|, |l2|))
// S: O(max(|l1|, |l2|))
const
addTwoNumbers
=
(
l1
,
l2
)
=>
{
let
p1
=
l1
;
let
p2
=
l2
;
let
carry
=
0
;
let
result_node
=
new
ListNode
(
-
1
)
;
let
pointer
=
result_node
;
while
(
p1
||
p2
||
carry
!==
0
)
{
let
sum
=
0
;
if
(
p1
!==
null
&&
p2
!==
null
)
{
sum
=
p1
.
val
+
p2
.
val
;
p1
=
p1
.
next
;
p2
=
p2
.
next
;
}
else
if
(
p1
!==
null
)
{
sum
=
p1
.
val
;
p1
=
p1
.
next
;
}
else
if
(
p2
!==
null
)
{
sum
=
p2
.
val
;
p2
=
p2
.
next
;
}
if
(
carry
!==
0
)
{
sum
=
sum
+
carry
;
carry
=
0
;
}
if
(
sum
>
9
)
{
carry
=
Math
.
floor
(
sum
/
10
)
;
sum
=
sum
%
10
;
}
pointer
.
next
=
new
ListNode
(
sum
)
;
pointer
=
pointer
.
next
;
}
return
result_node
.
next
;
}
;
Back
|
FazBrowse Home
|
New Git URL