[LeetCode] Merge k Sorted Lists

Source: Internet
Author: User

[Cpp]
Struct ListNode {
Int val;
ListNode * next;
ListNode (int x): val (x), next (NULL ){}
};
 
Class Solution {
// O (nlogk)
// Using priority_queue to choose current minimum node
// Node: keep priority_queue free of NULL pointer
Public:
Struct PQNode
{
Int index;
ListNode * ptr;
PQNode (int _ index = 0, ListNode * _ ptr = NULL): index (_ index), ptr (_ ptr ){}
};
Struct Comp
{
Bool operator () (const PQNode & lhs, const PQNode & rhs) const
{
Return lhs. ptr-> val> rhs. ptr-> val;
}
};
ListNode * mergeKLists (vector <ListNode *> & lists ){
// Start typing your C/C ++ solution below
// Do not write int main () function
Priority_queue <PQNode, vector <PQNode>, Comp> pq;
ListNode * head = NULL;
ListNode * cur = NULL;
Do
{
For (int I = 0; I <lists. size (); ++ I)
{
If (lists [I]! = NULL)
{
Pq. push (PQNode (I, lists [I]);
Lists [I] = lists [I]-> next;
}
}
If (pq. empty () break;
If (head = NULL)
Head = pq. top (). ptr;
Else
Cur-> next = pq. top (). ptr;
Cur = pq. top (). ptr;
Int curIdx = pq. top (). index;
Pq. pop ();
If (lists [curIdx]! = NULL)
{
Pq. push (PQNode (curIdx, lists [curIdx]);
Lists [curIdx] = lists [curIdx]-> next;
}
} While (true );
If (cur! = NULL) cur-> next = NULL;
Return head;
}
};

Struct ListNode {
Int val;
ListNode * next;
ListNode (int x): val (x), next (NULL ){}
};

Class Solution {
// O (nlogk)
// Using priority_queue to choose current minimum node
// Node: keep priority_queue free of NULL pointer
Public:
Struct PQNode
{
Int index;
ListNode * ptr;
PQNode (int _ index = 0, ListNode * _ ptr = NULL): index (_ index), ptr (_ ptr ){}
};
Struct Comp
{
Bool operator () (const PQNode & lhs, const PQNode & rhs) const
{
Return lhs. ptr-> val> rhs. ptr-> val;
}
};
ListNode * mergeKLists (vector <ListNode *> & lists ){
// Start typing your C/C ++ solution below
// Do not write int main () function
Priority_queue <PQNode, vector <PQNode>, Comp> pq;
ListNode * head = NULL;
ListNode * cur = NULL;
Do
{
For (int I = 0; I <lists. size (); ++ I)
{
If (lists [I]! = NULL)
{
Pq. push (PQNode (I, lists [I]);
Lists [I] = lists [I]-> next;
}
}
If (pq. empty () break;
If (head = NULL)
Head = pq. top (). ptr;
Else
Cur-> next = pq. top (). ptr;
Cur = pq. top (). ptr;
Int curIdx = pq. top (). index;
Pq. pop ();
If (lists [curIdx]! = NULL)
{
Pq. push (PQNode (curIdx, lists [curIdx]);
Lists [curIdx] = lists [curIdx]-> next;
}
} While (true );
If (cur! = NULL) cur-> next = NULL;
Return head;
}
};

 

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.