Skip to content

Commit ba00491

Browse files
committed
mergesort LL
1 parent 34cb2e6 commit ba00491

File tree

2 files changed

+100
-1
lines changed

2 files changed

+100
-1
lines changed

DSA Crack Sheet/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@
136136
- [Intersection of two sorted Linked lists](https://practice.geeksforgeeks.org/problems/intersection-of-two-sorted-linked-lists/1# "view question") - [Cpp Solution](./solutions/Intersection%20of%20two%20sorted%20Linked%20lists.cpp)
137137
- [Intersection Point in Y Shapped Linked Lists](https://practice.geeksforgeeks.org/problems/intersection-point-in-y-shapped-linked-lists/1 "view question") - [Cpp Solution](./solutions/Intersection%20Point%20in%20Y%20Shapped%20Linked%20Lists.cpp)
138138
- [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/ "view question") - [Cpp Solution](./solutions/Middle%20of%20the%20Linked%20List.cpp)
139-
- []( "view question") - [Cpp Solution](./solutions/.cpp)
139+
- [Merge Sort for Linked List](https://practice.geeksforgeeks.org/problems/sort-a-linked-list/1# "view question") - [Cpp Solution](./solutions/Merge%20Sort%20for%20Linked%20List.cpp)
140140
- []( "view question") - [Cpp Solution](./solutions/.cpp)
141141

142142
### Binary Tree
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
Merge Sort for Linked List
3+
==========================
4+
5+
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
6+
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
7+
8+
Example 1:
9+
Input:
10+
N = 5
11+
value[] = {3,5,2,4,1}
12+
Output: 1 2 3 4 5
13+
Explanation: After sorting the given
14+
linked list, the resultant matrix
15+
will be 1->2->3->4->5.
16+
17+
Example 2:
18+
Input:
19+
N = 3
20+
value[] = {9,15,0}
21+
Output: 0 9 15
22+
Explanation: After sorting the given
23+
linked list , resultant will be
24+
0->9->15.
25+
Your Task:
26+
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
27+
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
28+
29+
Expected Time Complexity: O(N*Log(N))
30+
Expected Auxiliary Space: O(N)
31+
32+
Constraints:
33+
1 <= T <= 100
34+
1 <= N <= 105
35+
*/
36+
37+
Node *midNode(Node *head)
38+
{
39+
auto fast = head, slow = head;
40+
while (fast && fast->next)
41+
{
42+
fast = fast->next->next;
43+
slow = slow->next;
44+
}
45+
return slow;
46+
}
47+
48+
Node *merge(Node *head1, Node *head2)
49+
{
50+
Node *ans = new Node(-1);
51+
auto temp = ans, temp1 = head1, temp2 = head2;
52+
53+
while (temp1 && temp2)
54+
{
55+
if (temp1->data < temp2->data)
56+
{
57+
auto next = temp1->next;
58+
temp->next = temp1;
59+
temp1->next = NULL;
60+
temp = temp->next;
61+
temp1 = next;
62+
}
63+
else
64+
{
65+
auto next = temp2->next;
66+
temp->next = temp2;
67+
temp2->next = NULL;
68+
temp = temp->next;
69+
temp2 = next;
70+
}
71+
}
72+
73+
if (temp1)
74+
temp->next = temp1;
75+
if (temp2)
76+
temp->next = temp2;
77+
78+
return ans->next;
79+
}
80+
81+
Node *mergeSort(Node *head)
82+
{
83+
if (!head || !head->next)
84+
return head;
85+
86+
auto mid = midNode(head);
87+
auto temp = head;
88+
89+
while (temp->next != mid)
90+
temp = temp->next;
91+
temp->next = NULL;
92+
93+
auto head2 = mid;
94+
95+
head = mergeSort(head);
96+
head2 = mergeSort(head2);
97+
98+
return merge(head, head2);
99+
}

0 commit comments

Comments
 (0)