LeeCode (heap, linked list) 23_ merge K ascending linked lists

LeeCode (heap, linked list) 23_ merge K ascending linked lists

Topic:
Give you an array of linked lists, each of which has been arranged in ascending order.

Please merge all linked lists into an ascending linked list and return the merged linked list.

Example 1:

Input: lists = [[1,4,5],[1,3,4],[2,6]]
Output: [1,1,2,3,4,4,5,6]
Explanation: Linked list array As follows:
[
1->4->5,
1->3->4,
2->6
]
Combine them into an ordered linked list.
1->1->2->3->4->4->5->6
Example 2:

Input: lists = []
Output: []
Example 3:

Input: lists = [[]]
Output: []

prompt:

k == lists.length
0 <= k <= 10^4
0 <= lists[i].length <= 500
-10^4 <= lists[i][j] <= 10^4
lists[i] 按 升序 排列
lists[i].length 的总和不超过 10^4

Source: LeetCode (LeetCode)
Link: https://leetcode-cn.com/problems/merge-k-sorted-lists
Copyright is owned by LeetCode . For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

Problem-solving ideas:

  1. First clarify the idea of ​​merging two linked lists into one linked list. First, create a head (ListNode type) to save the head of the merged linked list. The val and next of the head can be left without values. After the merge is completed, return to head.next.
  2. Then create a tail to record the position before the next insertion position, that is, the last ListNode that has been merged so far.
  3. Create two Ptrs, aPtr and bPtr, respectively, pointing to the first ListNode of the linked list A and the linked list B that are not merged. Use while to merge loops, each loop tail.next=aPtr or bPtr (judging val) and then tail and aPtr or bPtr point to the next one.
  4. After the loop is over, it is judged whether the linked list A and the linked list B are empty, and all the remaining elements are merged if they are not empty.

Merging method 1: Sequential merging
We can think of a simplest method: use a variable ans to maintain and merge the linked list, the i-th loop merges the i-th linked list with ans, and the answer is stored in ans.
Java code:


class ListNode {
    
    
	 int val;
     ListNode next;
     ListNode() {
    
    }
     ListNode(int val) {
    
     this.val = val; }
     ListNode(int val, ListNode next) {
    
     this.val = val; this.next = next; }
 }

public class 合并两个有序链表 {
    
    
	
	public ListNode mergeKLists(ListNode[] lists) {
    
    
		ListNode ans = null;
		for(int i=0;i<lists.length;i++){
    
    
			ans = mergeTwoLists(ans,lists[i]);
		}
		return ans;
	}
			
	public ListNode mergeTwoLists(ListNode a, ListNode b) {
    
    
		if(a == null || b == null){
    
    
			return a!=null?a:b;
		}
		
		ListNode head = new ListNode(0);
		ListNode tail = head,aPtr = a, bPtr = b;
		while(aPtr!=null && bPtr!=null){
    
    
			if(aPtr.val<bPtr.val){
    
    
				tail.next = aPtr;
				aPtr = aPtr.next;
			}else{
    
    
				tail.next = bPtr;
				bPtr = bPtr.next;
			}
			tail = tail.next;
		}
		tail.next = (aPtr!=null?aPtr:bPtr);
		return head.next;	
	}
}

Consolidation method 2: Divide and conquer merger


class ListNode {
    
    
	 int val;
     ListNode next;
     ListNode() {
    
    }
     ListNode(int val) {
    
     this.val = val; }
     ListNode(int val, ListNode next) {
    
     this.val = val; this.next = next; }
 }

public class 合并两个有序链表 {
    
    
	
	public ListNode mergeKLists(ListNode[] lists) {
    
    
        return merge(lists,0,lists.length);
    }

	public ListNode merge(ListNode[] lists, int l, int r) {
    
    
		if(l==r)
			return lists[l];
		if(l>r)
			return null;
		int mid = (l+r)/2;
		return mergeTwoLists(merge(lists,l,mid),merge(lists,mid+1,r));
	}
		
	public ListNode mergeTwoLists(ListNode a, ListNode b) {
    
    
		if(a == null || b == null){
    
    
			return a!=null?a:b;
		}
		
		ListNode head = new ListNode(0);
		ListNode tail = head,aPtr = a, bPtr = b;
		while(aPtr!=null && bPtr!=null){
    
    
			if(aPtr.val<bPtr.val){
    
    
				tail.next = aPtr;
				aPtr = aPtr.next;
			}else{
    
    
				tail.next = bPtr;
				bPtr = bPtr.next;
			}
			tail = tail.next;
		}
		tail.next = (aPtr!=null?aPtr:bPtr);
		return head.next;
		
	}

}

Guess you like

Origin blog.csdn.net/u013456390/article/details/111831422