-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode0021.java
More file actions
65 lines (55 loc) · 1.79 KB
/
Copy pathLeetCode0021.java
File metadata and controls
65 lines (55 loc) · 1.79 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
/* Merge Two Sorted Lists
* Input: 1->2->4, 1->3->4
* Output: 1->1->2->3->4->4
* */
public class LeetCode0021 {
public static void main(String args[]) {
int[] input1 = new int[]{9, 9, 9, 9};
ListNode l1 = buildListNode(input1);
int[] input2 = new int[]{1, 3, 4};
ListNode l2 = buildListNode(input2);
ListNode output = mergeTwoLists(l1, l2);
while (output != null) {
System.out.println(output.val);
output = output.next;
}
}
public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null) return l2;
if (l2 == null) return l1;
ListNode dummyHead = new ListNode(0);
ListNode mergedList = dummyHead;
while (l1 != null && l2 != null) {
if (l1.val <= l2.val) {
mergedList.next = l1;
l1 = l1.next;
} else {
mergedList.next = l2;
l2 = l2.next;
}
mergedList = mergedList.next;
}
if (l2 != null)
mergedList.next = l2;
if (l1 != null)
mergedList.next = l1;
return dummyHead.next;
}
private static ListNode buildListNode(int[] input) {
ListNode first = null, last = null, newNode;
if (input.length > 0) {
for (int i = 0; i < input.length; i++) {
newNode = new ListNode(input[i]);
newNode.next = null;
if (first == null) {
first = newNode;
last = newNode;
} else {
last.next = newNode;
last = newNode;
}
}
}
return first;
}
}