Leetcode 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. the solution to merging k sorting lists is to extract k elements for heap sorting. Each time the smallest elements are extracted, you can insert them into the linked list to note that this question uses the priority queue of c ++, the underlying layer of the priority queue is implemented by heap. In this topic, a special test case is [{}]. Note that when the vector is empty, it is represented as [] instead of [{}]. while the latter is a vector with a size of 1, where the element is a null pointer into the priority queue to determine whether it is null into the priority queue element is ListNode *, rather than int, it is mainly to keep the address of the original linked list, to avoid address failure, and put it into int, and then re-new ListNode (int) overhead is relatively large. define a comparison function object for ListNode * in the priority queue. If this parameter is not specified, the comparison is based on a large address. Small copy code class Solution {public: struct cmp {bool operator () (const ListNode * a, const ListNode * B) const {return a-> val> B-> val ;}}; ListNode * mergeKLists (vector <ListNode *> & lists) {if (lists. empty () return NULL; priority_queue <ListNode *, vector <ListNode *>, cmp> pq; ListNode * head = new ListNode (0), * pre = head; for (int I = 0; I <lists. size (); ++ I) {if (lists [I]) pq. push (lists [I]);} while (! Pq. empty () {ListNode * node = pq. top (); pq. pop (); if (node-> next) pq. push (node-> next); pre-> next = node; pre = node;} pre-> next = NULL; return head-> next ;}}; the second method of copying code is the binary method. Each time two ListNode * is obtained, the queue is used in the merge program, and two elements are extracted from the queue each time and then merged, add the merged elements to the queue again until there is only one small element in the queue. The Code class Solution {public: ListNode * mergeLists (ListNode * p, ListNode * q) can be copied) {ListNode * head = new ListNode (0), * pre = head; while (p & q) {if (p-> val <q-> val) {ListNode * tmp = p-> next; p-> next = pre-> next; pre-> next = p; pre = p; p = tmp ;} else {ListNode * tmp = q-> next; q-> next = pre-> next; pre-> next = q; pre = q; q = tmp ;}} pre-> next = NULL; if (p) pre-> next = p; if (q) pre-> next = q; return head-> next ;} listNode * mergeKLists (vector <ListNode *> & lists) {if (lists. empty () return NULL; queue <ListNode *> que; for (int I = 0; I <lists. size (); ++ I) {if (lists [I]) que. push (lists [I]);} while (que. size ()> 1) {ListNode * p = que. front (); que. pop (); ListNode * q = que. front (); que. pop (); que. push (mergeLists (p, q);} return que. front ();}};

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.