Skip to content

Commit a10ac1a

Browse files
committed
Day 33 Merge sort of linked list
1 parent b70127c commit a10ac1a

File tree

2 files changed

+127
-3
lines changed

2 files changed

+127
-3
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
| Current Status| Stats |
66
| :------------: | :----------: |
7-
| Total Problems | 48 |
8-
| Current Streak | 31 |
9-
| Longest Streak | 31 ( August 17, 2015 - September 16, 2015 ) |
7+
| Total Problems | 50 |
8+
| Current Streak | 33 |
9+
| Longest Streak | 33 ( August 17, 2015 - September 18, 2015 ) |
1010

1111
</center>
1212

@@ -25,6 +25,8 @@
2525
| Determine the intersection(merging) point of two given linked list.| [findIntersectionPointOfLists.cpp](linked_list_problems/findIntersectionPointOfLists.cpp)|
2626
| Clone a linkedlist which has next and an random pointer, Space Complexity - O(1). | [cloneListWithRandomPtr.cpp](linked_list_problems/cloneListWithRandomPtr.cpp)|
2727
| Given a sorted linked list with duplicates, remove duplicates in one iteration. | [removeDuplicatesFromSortedList.cpp](linked_list_problems/removeDuplicatesFromSortedList.cpp)|
28+
| Find the nth node of linked list from last. | [nthToLastNode.cpp] (linked_list_problems/nthToLastNode.cpp) |
29+
| Sort a linked list using merge sort | [merge_sort.cpp] (linked_list_problems/merge_sort.cpp) |
2830

2931
### Include
3032
Include contains single header implementation of data structures and some algorithms.

linked_list_problems/merge_sort.cpp

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#include <iostream>
2+
3+
struct Node {
4+
int data;
5+
Node * next;
6+
Node ( int d ) : data{ d }, next{ nullptr } { }
7+
};
8+
9+
void printList( Node * head ) {
10+
while( head ) {
11+
std::cout << head->data << "-->";
12+
head = head->next;
13+
}
14+
std::cout << "NULL" << std::endl;
15+
}
16+
17+
void push( Node * & head, int data )
18+
{
19+
Node * newNode = new Node(data);
20+
if ( head == nullptr ) {
21+
head = newNode;
22+
} else {
23+
Node * temp = head;
24+
while ( temp->next != nullptr ) {
25+
temp = temp->next;
26+
}
27+
temp->next = newNode;
28+
}
29+
}
30+
31+
void splitList( Node * head, Node * & list1, Node * & list2 )
32+
{
33+
if ( head == nullptr || head->next == nullptr ) {
34+
list1 = head;
35+
list2 = nullptr;
36+
return;
37+
}
38+
Node * fast = head->next;
39+
Node * slow = head;
40+
while( fast ) {
41+
fast = fast->next;
42+
if( fast ) {
43+
slow = slow->next;
44+
fast = fast->next;
45+
}
46+
}
47+
list1 = head;
48+
list2 = slow->next;
49+
slow->next = nullptr;
50+
}
51+
52+
53+
void moveNode( Node * & dstNode, Node * & srcNode )
54+
{
55+
if (srcNode == nullptr) {
56+
return;
57+
}
58+
Node * nextNode = srcNode->next;
59+
dstNode->next = srcNode;
60+
dstNode = srcNode;
61+
srcNode = nextNode;
62+
}
63+
64+
65+
Node * sortedMerge(Node * list1, Node * list2)
66+
{
67+
if (list1 == nullptr) {
68+
return list2;
69+
}
70+
71+
if (list2 == nullptr) {
72+
return list1;
73+
}
74+
75+
Node dummy(0);
76+
Node * tailNode = &dummy;
77+
dummy.next = nullptr;
78+
79+
while( list1 && list2 ) {
80+
if (list1->data <= list2->data){
81+
moveNode(tailNode, list1);
82+
} else {
83+
moveNode(tailNode, list2);
84+
}
85+
}
86+
while(list2) {
87+
moveNode(tailNode, list2);
88+
}
89+
while(list1) {
90+
moveNode(tailNode, list1);
91+
}
92+
return (dummy.next);
93+
}
94+
95+
void mergeSort( Node * & head ) {
96+
if (head == nullptr || head->next == nullptr) {
97+
return;
98+
}
99+
Node * list1 = nullptr;
100+
Node * list2 = nullptr;
101+
splitList(head, list1, list2);
102+
mergeSort(list1);
103+
mergeSort(list2);
104+
head = sortedMerge(list1, list2);
105+
}
106+
107+
int main()
108+
{
109+
Node * head = nullptr;
110+
push(head, 5);
111+
push(head, 1);
112+
push(head, 3);
113+
push(head, 6);
114+
push(head, 4);
115+
push(head, 2);
116+
std::cout << "Before Sorting :";
117+
printList(head);
118+
mergeSort(head);
119+
std::cout << "After Sorting :";
120+
printList(head);
121+
return 1;
122+
}

0 commit comments

Comments
 (0)