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

Data Structures

struct  Compare
 

Public Member Functions

ListNodemergeTwoLists (ListNode *list1, ListNode *list2)
 

Detailed Description

Definition at line 17 of file main.cpp.

Member Function Documentation

◆ mergeTwoLists()

ListNode * Solution::mergeTwoLists ( ListNode list1,
ListNode list2 
)
inline

Definition at line 28 of file main.cpp.

29 {
30 if (!list1 && !list2)
31 return NULL;
32 else if (!list1)
33 return list2;
34 else if (!list2)
35 return list1;
36 priority_queue<ListNode*, vector<ListNode*>, Compare> min_heap;
37 ListNode* current = list1;
38 while (current != NULL)
39 {
40 min_heap.push(current);
41 current = current->next;
42 }
43 current = list2;
44 while (current != NULL)
45 {
46 min_heap.push(current);
47 current = current->next;
48 }
49 ListNode* head = min_heap.top();
50 min_heap.pop();
51 current = head;
52 while (!min_heap.empty())
53 {
54 current->next = min_heap.top(); // dereferencing previous order
55 min_heap.pop();
56 current = current->next;
57 }
58 current->next = NULL;
59 return head;
60 }
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: