/
_0021_MergeTwoSortedLists.java
108 lines (99 loc) · 2.6 KB
/
_0021_MergeTwoSortedLists.java
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
package com.diguage.algorithm.leetcode;
import com.diguage.algorithm.util.ListNode;
import java.util.Arrays;
import java.util.Objects;
import static com.diguage.algorithm.util.ListNodeUtils.build;
import static com.diguage.algorithm.util.ListNodeUtils.isOrder;
/**
* = 21. Merge Two Sorted Lists
*
* https://leetcode.com/problems/merge-two-sorted-lists/[Merge Two Sorted Lists - LeetCode]
*
* Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
*
* .Example:
* [source]
* ----
* Input: 1->2->4, 1->3->4
* Output: 1->1->2->3->4->4
* ----
*
* @author D瓜哥, https://www.diguage.com/
* @since 2019-07-26 08:49
*/
public class _0021_MergeTwoSortedLists {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (Objects.isNull(l1) && Objects.isNull(l2)) {
return null;
}
ListNode dummy = new ListNode();
ListNode tail = dummy;
while (Objects.nonNull(l1) && Objects.nonNull(l2)) {
if (l1.val < l2.val) {
tail.next = l1;
l1 = l1.next;
} else {
tail.next = l2;
l2 = l2.next;
}
tail = tail.next;
}
if (Objects.isNull(l1)) {
tail.next = l2;
}
if (Objects.isNull(l2)) {
tail.next = l1;
}
return dummy.next;
}
/**
* Runtime: 1 ms, faster than 28.15% of Java online submissions for Merge Two Sorted Lists.
*
* Memory Usage: 39.8 MB, less than 14.45% of Java online submissions for Merge Two Sorted Lists.
*/
public ListNode mergeTwoLists1(ListNode l1, ListNode l2) {
if (Objects.isNull(l1)) {
return l2;
}
if (Objects.isNull(l2)) {
return l1;
}
ListNode result = null;
ListNode p1 = l1;
ListNode p2 = l2;
ListNode tail = null;
while (Objects.nonNull(p1) && Objects.nonNull(p2)) {
int v1 = p1.val;
int v2 = p2.val;
ListNode temp = null;
if (v1 < v2) {
temp = p1;
p1 = p1.next;
} else {
temp = p2;
p2 = p2.next;
}
if (Objects.isNull(tail)) {
result = temp;
tail = temp;
} else {
tail.next = temp;
tail = temp;
}
}
if (Objects.isNull(p1)) {
tail.next = p2;
}
if (Objects.isNull(p2)) {
tail.next = p1;
}
return result;
}
public static void main(String[] args) {
_0021_MergeTwoSortedLists solution = new _0021_MergeTwoSortedLists();
ListNode l1 = build(Arrays.asList());
ListNode l2 = build(Arrays.asList());
ListNode r1 = solution.mergeTwoLists(l1, l2);
System.out.println(isOrder(r1));
}
}