【Leetcode】Merge K Sorted Linked List
2015-07-16 21:39
281 查看
【题目】
Merge k sorted Linked Lists and return it as one sorted lists. Analyze and describe its complexity .
【思路】
leetcode clean book
【代码】 private static final Comparator<ListNode> listComparator = new Comparator<ListNode>(){
public int compare(ListNode x, ListNode y){
return x.val - y.val;
}
};
public static ListNode mergeK(List<ListNode> list){
if(list.isEmpty()) return null;
Queue<ListNode> queue = new PriorityQueue<>(list.size(),listComparator);
for(ListNode node : list){
if(node!=null){
queue.add(node);
}
}
ListNode dummy = new ListNode(0);
ListNode p = dummy;
while(!queue.isEmpty()){
ListNode node = queue.poll();
p.next = node;
p=p.next;
if(node.next!=null){
queue.add(node.next);
}
}
return dummy.next;
}
public static ListNode mergeK2(List<ListNode> list){
if (list == null) return null;
int end = list.size() - 1;
while(end > 0){
int start = 0;
while(start < end){
list.set(start, merge2List(list.get(start),list.get(end)));
start++;
end--;
}
}
return list.get(0);
}
public static ListNode merge2List(ListNode l1, ListNode l2){
ListNode dummy = new ListNode(0);
ListNode cur = dummy;
ListNode p = l1, q = l2;
while(p != null && q != null){
if(p.val > q.val ){
cur.next = q;
q = q.next;
}else{
cur.next = p;
p = p.next;
}
cur = cur.next;
}
if(p!=null) cur.next = p;
if(q!=null) cur.next = q;
return dummy.next;
}
Merge k sorted Linked Lists and return it as one sorted lists. Analyze and describe its complexity .
【思路】
leetcode clean book
【代码】 private static final Comparator<ListNode> listComparator = new Comparator<ListNode>(){
public int compare(ListNode x, ListNode y){
return x.val - y.val;
}
};
public static ListNode mergeK(List<ListNode> list){
if(list.isEmpty()) return null;
Queue<ListNode> queue = new PriorityQueue<>(list.size(),listComparator);
for(ListNode node : list){
if(node!=null){
queue.add(node);
}
}
ListNode dummy = new ListNode(0);
ListNode p = dummy;
while(!queue.isEmpty()){
ListNode node = queue.poll();
p.next = node;
p=p.next;
if(node.next!=null){
queue.add(node.next);
}
}
return dummy.next;
}
public static ListNode mergeK2(List<ListNode> list){
if (list == null) return null;
int end = list.size() - 1;
while(end > 0){
int start = 0;
while(start < end){
list.set(start, merge2List(list.get(start),list.get(end)));
start++;
end--;
}
}
return list.get(0);
}
public static ListNode merge2List(ListNode l1, ListNode l2){
ListNode dummy = new ListNode(0);
ListNode cur = dummy;
ListNode p = l1, q = l2;
while(p != null && q != null){
if(p.val > q.val ){
cur.next = q;
q = q.next;
}else{
cur.next = p;
p = p.next;
}
cur = cur.next;
}
if(p!=null) cur.next = p;
if(q!=null) cur.next = q;
return dummy.next;
}
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解
- LeetCode [Day 5] Longest Palindromic Substring 题解
- LeetCode [Day 6] ZigZag Conversion 题解