-
Notifications
You must be signed in to change notification settings - Fork 0
/
LLmerge_sort.java
109 lines (91 loc) · 2.58 KB
/
LLmerge_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
100
101
102
103
104
105
106
107
108
109
public class LLmerge_sort {
public static class Node {
int data;
Node next;
public Node(int data){
this.data = data;
this.next = null;
}
}
public static Node head;
public static Node tail;
public static void add_first(int data){
Node newNode = new Node(data);
if (head == null) {
head = newNode;
tail = newNode;
return;
}
newNode.next = head;
head = newNode;
return;
}
public static Node find_node(Node head){
Node slow = head;
Node fast = head.next;
while (fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
public static Node merge(Node head1, Node head2){
Node mergedLL = new Node(-1);
Node temp = mergedLL;
while (head1 != null && head2!=null){
if (head1.data <= head2.data){
temp.next = head1;
head1 = head1.next;
temp = temp.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 mergedLL.next;
}
public static Node merge_sort(Node head){
if (head == null || head.next == null){
return head;
}
Node mid = find_node(head);
Node righthead = mid.next;
mid.next = null;
Node left_half = merge_sort(head);
Node right_half = merge_sort(righthead);
return merge(left_half, right_half);
}
public static void mergeSort(){
head = merge_sort(head);
}
public static void print(){
Node temp = head;
while (temp != null) {
System.out.print(temp.data + " --> ");
temp = temp.next;
}
System.out.print("null");
}
public static void main(String[] args) {
LLmerge_sort l1 = new LLmerge_sort();
l1.add_first(2);
l1.add_first(5);
l1.add_first(6);
l1.add_first(8);
l1.print();
l1.mergeSort();
System.out.println();
l1.print();
}
}