-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.java
More file actions
48 lines (39 loc) · 1.15 KB
/
MergeSort.java
File metadata and controls
48 lines (39 loc) · 1.15 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
package LinkedLists;
/**
* V.IMP Question, One of the most important one.
*/
public class MergeSort {
public static Node sortList(Node head) {
if (head == null || head.next == null)
return head;
// Mid of the list and divide into two.
Node prev = null, slow = head, fast = head;
while (fast != null && fast.next != null) {
prev = slow;
slow = slow.next;
fast = fast.next.next;
}
prev.next = null;
Node list1 = sortList(head);
Node list2 = sortList(slow);
return merge(list1, list2);
}
public static Node merge(Node list1, Node list2) {
Node l = new Node(0), p = l;
while (list1 != null && list2 != null) {
if (list1.data < list2.data) {
p.next = list1;
list1 = list1.next;
} else {
p.next = list2;
list2 = list2.next;
}
p = p.next;
}
if (list1 != null) p.next = list1;
if (list2 != null) p.next = list2;
return l.next;
}
public static void main(String[] args) {
}
}