Merge k sorted linked lists and return it as one sorted list.
C++
// C++ implementation to merge k sorted linked lists
// | Using MIN HEAP method
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
// 'compare' function used to build up the
// priority queue
struct compare {
bool operator()(struct Node* a, struct Node* b)
{
return a->data > b->data;
}
};
// function to merge k sorted linked lists
struct Node* mergeKSortedLists(struct Node* arr[], int k)
{
struct Node *head = NULL, *last;
// priority_queue 'pq' implemeted as min heap with the
// help of 'compare' function
priority_queue<Node*, vector<Node*>, compare> pq;
// push the head nodes of all the k lists in 'pq'
for (int i = 0; i < k; i++)
if (arr[i] != NULL)
pq.push(arr[i]);
// loop till 'pq' is not empty
while (!pq.empty()) {
// get the top element of 'pq'
struct Node* top = pq.top();
pq.pop();
// check if there is a node next to the 'top' node
// in the list of which 'top' node is a member
if (top->next != NULL)
// push the next node in 'pq'
pq.push(top->next);
// if final merged list is empty
if (head == NULL) {
head = top;
// points to the last node so far of
// the final merged list
last = top;
}
else {
// insert 'top' at the end of the merged list so far
last->next = top;
// update the 'last' pointer
last = top;
}
}
// head node of the required merged list
return head;
}
// function to print the singly linked list
void printList(struct Node* head)
{
while (head != NULL) {
cout << head->data << " ";
head = head->next;
}
}
// Utility function to create a new node
struct Node* newNode(int data)
{
// allocate node
struct Node* new_node = new Node();
// put in the data
new_node->data = data;
new_node->next = NULL;
return new_node;
}
// Driver program to test above
int main()
{
int k = 3; // Number of linked lists
int n = 4; // Number of elements in each list
// an array of pointers storing the head nodes
// of the linked lists
Node* arr[k];
// creating k = 3 sorted lists
arr[0] = newNode(1);
arr[0]->next = newNode(3);
arr[0]->next->next = newNode(5);
arr[0]->next->next->next = newNode(7);
arr[1] = newNode(2);
arr[1]->next = newNode(4);
arr[1]->next->next = newNode(6);
arr[1]->next->next->next = newNode(8);
arr[2] = newNode(0);
arr[2]->next = newNode(9);
arr[2]->next->next = newNode(10);
arr[2]->next->next->next = newNode(11);
// merge the k sorted lists
struct Node* head = mergeKSortedLists(arr, k);
// print the merged list
printList(head);
return 0;
}
Also in C++:
- Title
- list conda environments
- Category
- C++
- Title
- nearest integer rounding in c++
- Category
- C++
- Title
- hohw toparse a string in c++
- Category
- C++
- Title
- making random numbers in c++
- Category
- C++
- Title
- c++ string to integer without stoi
- Category
- C++
- Title
- merge sort in c++
- Category
- C++
- Title
- clear console c++
- Category
- C++
- Title
- friend function in c++
- Category
- C++
- Title
- map arduino
- Category
- C++
- Title
- strchr function in c++
- Category
- C++
- Title
- cpp how to create an object of template class
- Category
- C++
- Title
- pass vector by reference c++
- Category
- C++
- Title
- c++ char define
- Category
- C++
- Title
- first prime numbers
- Category
- C++
- Title
- c++ argv
- Category
- C++
- Title
- mysqli connect
- Category
- C++
- Title
- array syntax in c++
- Category
- C++
- Title
- c++ char if
- Category
- C++
- Title
- c++ read matttrix from text file
- Category
- C++
- Title
- pionter in c++
- Category
- C++
- Title
- phph date
- Category
- C++
- Title
- array<string, 7> c++
- Category
- C++
- Title
- tellg and seekg c++
- Category
- C++
- Title
- c++ switch case statement
- Category
- C++
- Title
- shortest path with bfs in c++
- Category
- C++
- Title
- factorion
- Category
- C++
- Title
- how to delete something in an array c++
- Category
- C++
- Title
- double ended queue in c++ stl
- Category
- C++
- Title
- remove element by index from vector c++
- Category
- C++
- Title
- rgb(100,100,100,0.5) validation c++
- Category
- C++
- Title
- passing array to function c++ pointer
- Category
- C++
- Title
- index string c++
- Category
- C++
- Title
- c++ scanf
- Category
- C++
- Title
- passing reference in c++
- Category
- C++
- Title
- c++ string to vector int
- Category
- C++
- Title
- select elements from array C++
- Category
- C++
- Title
- c++ variable arguments
- Category
- C++
- Title
- binary search in set c++
- Category
- C++
- Title
- in c, is class uppercase or lowercase
- Category
- C++
- Title
- inserting an element in an set c++
- Category
- C++
- Title
- file objects in c++
- Category
- C++
- Title
- primos menores que
- Category
- C++
- Title
- log base 10 c+_+
- Category
- C++
- Title
- best fit algorithm
- Category
- C++
- Title
- queue c++
- Category
- C++
- Title
- int to float c++
- Category
- C++
- Title
- variant hold type
- Category
- C++
- Title
- how to cout in c++
- Category
- C++
- Title
- clear file before writing c++
- Category
- C++
- Title
- reverse in vector c++
- Category
- C++
- Title
- convert string to stream c++
- Category
- C++
- Title
- euler phi gfg
- Category
- C++
- Title
- c++ convert int to double
- Category
- C++
- Title
- c++ string to stream
- Category
- C++
- Title
- hashset in c++
- Category
- C++
- Title
- pop from between string c++
- Category
- C++
- Title
- Operator overloading in C++ Programming
- Category
- C++
- Title
- c++ dereference a pointer
- Category
- C++
- Title
- c++ do while loop
- Category
- C++
- Title
- coping 2d vector in cpp
- Category
- C++
- Title
- how to remove maximum number of characters in c++ cin,ignore
- Category
- C++
- Title
- c++ base 10 to base 2
- Category
- C++
- Title
- print type cpp
- Category
- C++
- Title
- binary tree search
- Category
- C++
- Title
- find in string c++
- Category
- C++
- Title
- how to make string get spaces c++
- Category
- C++
- Title
- pointer related problems dangling/wild pointers c++
- Category
- C++
- Title
- split string at index c++
- Category
- C++
- Title
- c++ pi
- Category
- C++
- Title
- convert stirng to int c++
- Category
- C++
- Title
- ue4 c++ overlapping functions cpp setup
- Category
- C++
- Title
- what are the different ways to traverse a binary tree
- Category
- C++
- Title
- declaring 2d vector in c++
- Category
- C++
- Title
- cin.fail()
- Category
- C++
- Title
- fmod c++
- Category
- C++
- Title
- jump to case label c++
- Category
- C++
- Title
- c++ program for matrix addition
- Category
- C++
- Title
- c++ empty stream
- Category
- C++
- Title
- c++ compare char
- Category
- C++
- Title
- c++ constructor
- Category
- C++
- Title
- cpp nan value
- Category
- C++
- Title
- c++ parse int
- Category
- C++
- Title
- min heap priority queue c++
- Category
- C++
- Title
- what is order in of preeendence in float, int, char, bool
- Category
- C++
- Title
- find in set of pairs using first value cpp
- Category
- C++
- Title
- peak in c++
- Category
- C++
- Title
- never gonna give you up lyrics
- Category
- C++
- Title
- decimal to hex cpp
- Category
- C++
- Title
- pair in c++
- Category
- C++
- Title
- find in vector in c++
- Category
- C++
- Title
- cloud hosting
- Category
- C++
- Title
- sort function in cpp
- Category
- C++
- Title
- reverse a linked list using recursion
- Category
- C++
- Title
- SFML window
- Category
- C++
- Title
- c++ wait for user input
- Category
- C++
- Title
- is TLE means my code is correct but taking more time to computr
- Category
- C++
- Title
- object slicing in c++
- Category
- C++
- Title
- c++ how to return an empty vector
- Category
- C++
- Title
- mingw32/bin/ld.exe: C:\Users\mfrom\AppData\Local\Temp\ccSKcRks.o:PizzaPi.cpp:(.text$_ZN5PizzaC2Ev[__ZN5PizzaC2Ev]+0xa): undefined reference to `vtable for Pizza' collect2.exe: error: ld returned 1 exit status
- Category
- C++
- Title
- vector initialization c++
- Category
- C++