-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path27 April Done : Merge Sort on Doubly Linked
66 lines (66 loc) · 1.87 KB
/
27 April Done : Merge Sort on Doubly Linked
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
Question ::
Merge Sort on Doubly Linked List
Given Pointer/Reference to the head of a doubly linked list of n nodes, the task is to Sort the given doubly linked list using Merge Sort in both non-decreasing and non-increasing order.
Time Complexity: O(N*log(N))
Space Complexity : O(1)
Solution ::
class Solution {
Node *findMid(Node * head){
Node * slow = head;
Node * fast=head->next;
while(fast && fast->next){
fast = fast->next->next;
slow = slow->next;
}
return slow;
}
Node * merge(Node * left, Node * right){
if(left == NULL)return right;
if(right == NULL)return left;
Node * ans = new Node(-1);
Node * temp = ans;
while(left && right){
if(left->data < right->data){
temp->next =left;
left->prev = temp;
temp = left;
left = left->next;
}
else{
temp->next = right;
right->prev = temp;
temp = right;
right = right->next;
}
}
while(left){
temp->next =left;
left->prev = temp;
temp = left;
left = left->next;
}
while(right){
temp->next =right;
right->prev = temp;
temp = right;
right = right->next;
}
ans = ans->next;
ans->prev=NULL;
return ans;
}
Node * solve( struct Node * head ){
if(!head || !head->next)return head;
Node * mid = findMid(head);
Node * left = head;
Node * right = mid->next;
mid->next = NULL;
left = solve(left);
right = solve(right);
return merge(left , right);
}
public:
struct Node *sortDoubly(struct Node *head) {
return solve(head);
}
};