FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/Week_01/id_127/LeetCode_21_127.java at master · feixiangcode/algorithm · GitHub
feixiangcode
/
algorithm
Public
forked from
algorithm001/algorithm
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
algorithm
/
Week_01
/
id_127
/
LeetCode_21_127.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
73 lines (64 loc) · 1.71 KB
Breadcrumbs
algorithm
/
Week_01
/
id_127
/
LeetCode_21_127.java
Copy path
File metadata and controls
73 lines (64 loc) · 1.71 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class
Solution
{
public
ListNode
mergeTwoLists
(
ListNode
l1
,
ListNode
l2
) {
if
(
l1
==
null
)
return
l2
;
if
(
l2
==
null
)
return
l1
;
ListNode
cur1
=
l1
;
ListNode
cur2
=
l2
;
ListNode
mergeCur
=
null
;
ListNode
mergeHead
=
null
;
if
(
cur1
.
val
<=
cur2
.
val
) {
mergeCur
=
cur1
;
mergeHead
=
cur1
;
cur1
=
cur1
.
next
;
}
else
{
mergeCur
=
cur2
;
mergeHead
=
cur2
;
cur2
=
cur2
.
next
;
}
while
(
cur1
!=
null
&&
cur2
!=
null
) {
if
(
cur1
.
val
<=
cur2
.
val
) {
mergeCur
.
next
=
cur1
;
cur1
=
cur1
.
next
;
}
else
{
mergeCur
.
next
=
cur2
;
cur2
=
cur2
.
next
;
}
mergeCur
=
mergeCur
.
next
;
}
if
(
cur1
!=
null
) {
mergeCur
.
next
=
cur1
;
}
if
(
cur2
!=
null
) {
mergeCur
.
next
=
cur2
;
}
return
mergeHead
;
}
/**
* 递归思路没有想到,参考网上的相关答案,理解之后写出
*/
public
ListNode
mergeTowListsRecursive
(
ListNode
l1
,
ListNode
l2
) {
if
(
l1
==
null
)
return
l2
;
if
(
l2
==
null
)
return
l1
;
if
(
l1
.
val
<=
l2
.
val
) {
ListNode
head
=
l1
;
head
.
next
=
mergeTowListsRecursive
(
l1
.
next
,
l2
);
return
head
;
}
else
{
ListNode
head
=
l2
;
head
.
next
=
mergeTowListsRecursive
(
l1
,
l2
.
next
);
return
head
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL