-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathLinked_list_merge_sort.java
99 lines (87 loc) · 2.17 KB
/
Linked_list_merge_sort.java
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// class defining the node structure
class Node
{
int data;
Node next;
Node(int key)
{
this.data = key;
next = null;
}
}
class MergeSort
{
static Node mergeSort(Node head)
{
if (head.next == null)
return head;
Node mid = findMid(head);
Node head2 = mid.next;
mid.next = null;
Node newHead1 = mergeSort(head);
Node newHead2 = mergeSort(head2);
Node finalHead = merge(newHead1, newHead2);
return finalHead;
}
static Node merge(Node head1, Node head2)
{
Node merged = new Node(-1);
Node temp = merged;
while (head1 != null && head2 != null) {
if (head1.data < head2.data) {
temp.next = head1;
head1 = head1.next;
}
else {
temp.next = head2;
head2 = head2.next;
}
temp = temp.next;
}
while (head1 != null) {
temp.next = head1;
head1 = head1.next;
temp = temp.next;
}
while (head2 != null) {
temp.next = head2;
head2 = head2.next;
temp = temp.next;
}
return merged.next;
}
static Node findMid(Node head)
{
Node slow = head, fast = head.next;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
static void printList(Node head)
{
while (head != null) {
System.out.print(head.data + " ");
head = head.next;
}
}
public static void main(String[] args)
{
Node head = new Node(7);
Node temp = head;
temp.next = new Node(10);
temp = temp.next;
temp.next = new Node(5);
temp = temp.next;
temp.next = new Node(20);
temp = temp.next;
temp.next = new Node(3);
temp = temp.next;
temp.next = new Node(2);
temp = temp.next;
head = mergeSort(head);
System.out.print("\nSorted Linked List is: \n");
printList(head);
}
}