-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathMergeKLists.java
59 lines (54 loc) · 1.06 KB
/
MergeKLists.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
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode mergeKLists(ArrayList<ListNode> lists) {
// Start typing your Java solution below
// DO NOT write main() function
if (lists.size() == 0) {
return null;
}
ListNode p = lists.get(0);
//need to create a header for the list
ListNode head = new ListNode(0);
head.next = p;
for (int i = 1; i < lists.size(); i++) {
merge2Lists(head, lists.get(i));
}
return head.next;
}
public void merge2Lists(ListNode head, ListNode q){
ListNode p = head.next;
ListNode prev = head;
if (p == null) {
head.next = q;
}
while (p != null && q != null) {
if (p.val < q.val) {
if (p.next != null) {
prev = p;
p = p.next;
}
else {
p.next = q;
break;
}
}
else { //insert q before p
ListNode temp = q.next;
prev.next = q;
q.next = p;
prev = q;
q = temp;
}
}
}
}