"Leetcode" "hard" Merge K Sorted Lists

Source: Internet
Author: User

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.

Problem Solving Ideas:

1, first take out the first element of K list, each first element is the smallest element in the corresponding list, forming a minimum heap with k nodes; O (K*LOGK)

2. The top element of the heap is the smallest element of all K lists, pops it out and fills the top of the heap with the next node (if any) on the list on which the smallest element is located, and performs the next filter operation to rebuild the heap. O (LOGK)

3. Perform the second step continuously until the element in the heap is 0. O (NLOGK)

The final time complexity is O (NLOGK), K is the number of lists, n means all the list summary points;

Problem Solving Method 1:

Use C + + make_heap,push_heap,pop_heap functions;

Steps:

1. Create a new node Prehead and point to the first node of the final linked list; Create a new curnode pointer to manipulate the new linked list;

2, the first pointer of each list is loaded into a new vector V, note that the list is not empty;

3. Call the Make_heap function to construct piles of these first pointers; the greater comparison function needs to be written;

4, circular operation, as long as V is not empty:

(1) Remove the top of the heap and put it at the end of the new list;

(2) Pop_heap, exchange the current heap end-to-end elements, and filter the first element;

(3) Pop_back, delete the last element of V (the current smallest element);

(4) Push_back, adding the next element of the current smallest element to the heap tail;

(5) Push_heap, the stack tail element is filtered;

5. Return to prehead->next;

Code:

1 /**2 * Definition for singly-linked list.3 * struct ListNode {4 * int val;5 * ListNode *next;6 * ListNode (int x): Val (x), Next (NULL) {}7  * };8  */9 classSolution {Ten  Public: Onelistnode* mergeklists (vector<listnode*>& lists) {//Make_heap AListNode Prehead (0); -ListNode *curnode = &Prehead; -Vector<listnode*>v;  the          for(inti =0; I < lists.size (); i++){ -             if(Lists[i]) - V.push_back (Lists[i]); -         } +Make_heap (V.begin (), V.end (), Heapcomp);//Vector--Heap data strcture -      +          while(V.size () >0){ ACurnode->next =V.front (); at pop_heap (V.begin (), V.end (), heapcomp); - V.pop_back (); -Curnode = curnode->Next; -             if(curnode->next) { -V.push_back (curnode->next);  - push_heap (V.begin (), V.end (), heapcomp); in             } -         } to          +         returnPrehead.next; -     } the      *     Static BOOLHeapcomp (listnode* A, listnode*b) { $             returnA->val > B->Val;Panax Notoginseng     } -};

Problem Solving Method 2:

Build minimum heap using C + + priority_queue;

The steps are basically consistent with the above;

Code

1 /**2 * Definition for singly-linked list.3 * struct ListNode {4 * int val;5 * ListNode *next;6 * ListNode (int x): Val (x), Next (NULL) {}7  * };8  */9 classSolution {Ten  Public: One     structCMP { A         BOOL operator()(ConstListNode *n1,ConstListNode *n2) { -             returnN1->val > N2->Val; -         } the     }; -  -ListNode *mergeklists (Vector<listnode *> &lists) { -priority_queue<listnode*, Vector<listnode *>, cmp >Heap; +ListNode Pre_head (0); -ListNode *curnode = &Pre_head; +          for(inti =0; I < lists.size (); i++) { A             if(Lists[i]) at Heap.push (Lists[i]); -         } -                  -          while(!Heap.empty ()) { -Curnode->next =heap.top (); - Heap.pop (); inCurnode = curnode->Next; -             if(curnode->next) toHeap.push (curnode->next); +         } -          the         returnPre_head.next; *     } $};

"Leetcode" "hard" Merge K Sorted Lists

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.