-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSortLinkedList.java
More file actions
81 lines (80 loc) · 1.65 KB
/
MergeSortLinkedList.java
File metadata and controls
81 lines (80 loc) · 1.65 KB
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
//GFG - MergeSort for Linked Lists
class LNode{
int val;
LNode next;
String rep = "";
LNode(int val){
this.val = val;
next = null;
}
public String toString(){
return (val+"");
}
}
class List{
LNode head;
public void push(int val){
if(head == null){
head = new LNode(val);
return;
}
LNode node = new LNode(val);
node.next = head;
head = node;
}
public void push(int[] vals){
if(vals == null) return;
for(Integer i : vals){
push(i);
}
}
public String toString(){
StringBuilder rep = new StringBuilder();
LNode t = head;
while(t != null){
rep.append(t + " ");
t = t.next;
}
return rep.toString().trim();
}
}
public class MergeSortLinkedList {
public static void main(String args[]){
List list = new List();
int[] vals = {5, 4, 2, 3, 1};
list.push(vals);
MergeSort(list);
System.out.println(list);
}
public static void MergeSort(List list){
list.head = MergeSort(list.head);
}
public static LNode MergeSort(LNode head){
if(head == null || head.next == null) return head;
LNode slow, fast, prev;
slow = fast = prev = head;
while(fast != null && fast.next != null){
fast = fast.next.next;
prev = slow;
slow = slow.next;
}
prev.next = null;
LNode list1 = MergeSort(head);
LNode list2 = MergeSort(slow);
return sortedMerge(list1, list2);
}
public static LNode sortedMerge(LNode head1, LNode head2){
if(head1 == null) return head2;
if(head2 == null) return head1;
LNode result;
if(head1.val <= head2.val){
result = head1;
result.next = sortedMerge(head1.next,head2);
}
else{
result = head2;
result.next = sortedMerge(head1, head2.next);
}
return result;
}
}