Merge k Sorted Lists
Topic:
Merge sorted linked lists and return it as a new list. The new list should is made by splicing together the nodes of the first of the lists.
Test instructions
Merges a list of K-sorted lists.
Ideas:
1. Merge two at a time, merge k-1 times, but timeout time Limit exceeded,o (n^2) level
2. Similar merge sort O (Nlogn)
3. Use heap properties. In fact, each time you compare the corresponding first node of all the linked list, and then find the smallest, add to the new linked list, delete the smallest node of the original list, and then let the next node of the linked list of the deleted node come in to participate in the comparison. We use the heap to reduce the time complexity to the NLOGN level, as we have just said, to build a heap of all the first nodes of the list, and then delete the minimum time complexity in the heap is log n. After you delete it, make the next heap.
Code:
Idea 1: (timeout)
/** * Definition forsingly-linked list. * struct ListNode {* int val; * ListNode *Next; * ListNode (int x): Val (x),Next(NULL) {} * }; */class Solution {public:listnode* mergetwolists (ListNode *l1, ListNode *l2) {if(L1 = =NULL&& L2 = =NULL){return NULL; }Else if(L1 = =NULL&& L2! =NULL){returnL2; }Else if(L1! =NULL&& L2 = =NULL){returnL1; } ListNode *new_head =NULL;if(L1->val < L2->val) {new_head = L1; L1 = l1->Next; }Else{new_head = L2; L2 = l2->Next; } ListNode *p = New_head; while(L1! =NULL&& L2! =NULL){if(L1->val < L2->val) {p->Next= L1; p = p->Next; L1 = l1->Next; }Else{p->Next= L2; p = p->Next; L2 = l2->Next; } }if(L1! =NULL) {p->Next= L1; }Else if(L2! =NULL) {p->Next= L2; }returnNew_head; } listnode* mergeklists (vector<listnode*>& lists) {if(lists.size () = =0){return NULL; }Else if(lists.size () = =1){returnlists[0]; } ListNode *ret; for(int i = lists.size ()-1; i >0; -i) {ret = Mergetwolists (Lists[i], lists[i-1]); lists[i-1] = RET; }returnlists[0]; }};
Idea 2: (pass)
/** * Definition for singly-linked list. * struct ListNode {* int val; * ListNode *next; * ListNode (int x ): Val (x), Next (NULL) {}}; */classSolution { Public: listnode* mergetwolists (ListNode *l1, ListNode *l2) {if(L1 = = NULL && L2 = = null) {returnNULL; }Else if(L1 = = NULL && L2! = null) {returnL2; }Else if(L1! = null && L2 = = null) {returnL1; } ListNode *new_head = NULL;if(L1->val < L2->val) {new_head = L1; L1 = l1->next; }Else{new_head = L2; L2 = l2->next; } ListNode *p = New_head; while(L1! = null && L2! = null) {if(L1->val < L2->val) {p->next = L1; p = p->next; L1 = l1->next; }Else{p->next = L2; p = p->next; L2 = l2->next; } }if(L1! = NULL) {p->next = L1; }Else if(L2! = NULL) {p->next = L2; }returnNew_head; } listnode* mergeklists ( vector<ListNode*>& lists) {if(lists.size () = =0){returnNULL; }Else if(lists.size () = =1){returnlists[0]; }AutoLength = Lists.end ()-lists.begin ();AutoMID = Lists.begin () + (length-1)/2; vector<ListNode*>V1 (Lists.begin (), mid+1); vector<ListNode*>V2 (mid+1, Lists.end ());//The idea of merging sortListNode *l1 = mergeklists (v1); ListNode *l2 = mergeklists (v2);returnMergetwolists (L1, L2); }};
Leetcode23--Merge k Sorted Lists