From 3b3fdde97fcea3c8dcb127f97e52885da3b36e4f Mon Sep 17 00:00:00 2001 From: Tushar Nain <71440085+tm4578@users.noreply.github.com> Date: Sat, 15 Oct 2022 22:52:28 +0530 Subject: [PATCH] Added Merge Sort for Linked List Merge Sort to sort Linked List in efficient way using C++ --- LinkedList/c++/MergeSort_Linked_List.cpp | 88 ++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 LinkedList/c++/MergeSort_Linked_List.cpp diff --git a/LinkedList/c++/MergeSort_Linked_List.cpp b/LinkedList/c++/MergeSort_Linked_List.cpp new file mode 100644 index 0000000..3c53871 --- /dev/null +++ b/LinkedList/c++/MergeSort_Linked_List.cpp @@ -0,0 +1,88 @@ +#include +using namespace std; + +struct ListNode +{ + int val; + ListNode *next; + ListNode() : val(0), next(nullptr) {} + ListNode(int x) : val(x), next(nullptr) {} + ListNode(int x, ListNode *next) : val(x), next(next) {} +}; + +ListNode *newnode(int x, ListNode *nxt = NULL) +{ + ListNode *newnode = new ListNode; + newnode->val = x; + newnode->next = nxt; + return newnode; +} + +ListNode *mergeTwoLists(ListNode *list1, ListNode *list2) +{ + ListNode *head = new ListNode(); + ListNode *t = head; + + ListNode *itr1 = list1, *itr2 = list2; + + while (itr1 != NULL && itr2 != NULL) + { + if (itr1->val < itr2->val) + { + t->next = itr1; + itr1 = itr1->next; + } + else + { + t->next = itr2; + itr2 = itr2->next; + } + + t = t->next; + } + + if (itr1 != NULL) + t->next = itr1; + if (itr2 != NULL) + t->next = itr2; + + return head->next; +} + +int main() +{ +//demo list 1 + ListNode *t1, *t2, *t3, *t4, *t5; + t5 = new ListNode(9, NULL); + t4 = new ListNode(7, t5); + t3 = new ListNode(5, t4); + t2 = new ListNode(3, t3); + t1 = new ListNode(1, t2); + // t1->next = t2; + // t2->next = t3; + // t3->next = t4; + // t4->next = t5; + // t5->next = NULL; + +//demo list 2 + ListNode *s1, *s2, *s3, *s4, *s5; + s5 = new ListNode(10, NULL); + s4 = new ListNode(8, s5); + s3 = new ListNode(6, s4); + s2 = new ListNode(4, s3); + s1 = new ListNode(2, s2); + // s1->next = s2; + // s2->next = s3; + // s3->next = s4; + // s4->next = s5; + // s5->next = NULL; + + ListNode *h = mergeTwoLists(t1, s1); + while (h != NULL) + { + cout << h->val << " "; + h = h->next; + } + + return 0; +}