merge-k-sorted-lists 1.0.0
Merge k Sorted Lists
Loading...
Searching...
No Matches
Solution Class Reference

Data Structures

struct  Comparator
 

Public Member Functions

ListNodemergeKLists (vector< ListNode * > &lists)
 

Detailed Description

Definition at line 17 of file main.cpp.

Member Function Documentation

◆ mergeKLists()

ListNode * Solution::mergeKLists ( vector< ListNode * > &  lists)
inline

Definition at line 28 of file main.cpp.

29 {
30 if (lists.empty())
31 return NULL;
32 priority_queue<ListNode*, vector<ListNode*>, Comparator> min_heap;
33 for (ListNode* head: lists)
34 {
35 if (head == NULL)
36 continue;
37 ListNode* current = head;
38 while (current != NULL)
39 {
40 min_heap.push(current);
41 current = current->next;
42 }
43 }
44 if (min_heap.empty())
45 return NULL;
46 ListNode* head = min_heap.top();
47 min_heap.pop();
48 ListNode* current = head;
49 while (!min_heap.empty())
50 {
51 current->next = min_heap.top();
52 min_heap.pop();
53 current = current->next;
54 }
55 current->next = NULL;
56 return head;
57 }
Definition for singly-linked list.
Definition main.cpp:9
ListNode * next
Definition main.cpp:11

References ListNode::next.


The documentation for this class was generated from the following file: