-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathMergeTwoSortedLists.java
More file actions
128 lines (117 loc) · 3.41 KB
/
Copy pathMergeTwoSortedLists.java
File metadata and controls
128 lines (117 loc) · 3.41 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package merge;
// Source : https://leetcode.com/problems/merge-two-sorted-lists/
// Id : 21
// Author : Fanlu Hai | https://github.com/Fanlu91/FanluLeetcode
// Date : 2019-05-29
// Topic : Linked List, Recursion
// Level : Easy
// Other :
// Tips :
// Links : Must
// Result : 100.00% 79.05%
public class MergeTwoSortedLists {
// 100.00% 56.90%
public ListNode mergeTwoLists2(ListNode l1, ListNode l2) {
// public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null)
return l2;
if (l2 == null)
return l1;
ListNode sentinel = new ListNode(-1), cur = sentinel;
while (true) {
if (l1.val < l2.val) {
cur.next = l1;
cur = l1;
if (l1.next == null) { // if l1 is the last node
cur.next = l2;
break;
}
l1 = l1.next;
} else {
cur.next = l2;
cur = l2;
if (l2.next == null) {
cur.next = l1;
break;
}
l2 = l2.next;
}
}
return sentinel.next;
}
// 100.00% 0ms 79.05%
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null)
return l2;
if (l2 == null)
return l1;
if (l1.val < l2.val) {
l1.next = mergeTwoLists(l1.next, l2);
return l1;
} else { // 这里可以省掉else,不过带上代码可读性更强
l2.next = mergeTwoLists(l1, l2.next);
return l2;
}
}
// rewrite
public ListNode mergeTwoLists3(ListNode l1, ListNode l2) {
// public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null)
return l2;
if (l2 == null)
return l1;
/* ListNode sentinel = new ListNode(-1), cur = null;
if (l1.val > l2.val) {
cur = l2;
l2 = l2.next;
} else {
cur = l1;
l1 = l1.next;
}
sentinel.next = cur;*/
// cur = sentinel 就替代了上面的代码
ListNode sentinel = new ListNode(-1), cur = sentinel;
while (l1 != null && l2 != null) {
if (l1.val > l2.val) {
cur.next = l2;
l2 = l2.next;
} else {
cur.next = l1;
l1 = l1.next;
}
cur = cur.next;
}
cur.next = l1 == null ? l2 : l1;
return sentinel.next;
}
// practice
public ListNode mergeTwoLists4(ListNode list1, ListNode list2) {
// public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
ListNode sentinel = new ListNode(), cur = sentinel;
while (list1 != null && list2 != null) {
if (list1.val > list2.val) {
cur.next = list2;
cur = list2;
list2 = list2.next;
} else {
cur.next = list1;
cur = list1;
list1 = list1.next;
}
}
if (list1 == null)
cur.next = list2;
if (list2 == null)
cur.next = list1;
return sentinel.next;
}
class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int x) {
val = x;
}
}
}