Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1d726b2

Browse files
author
Ram swaroop
committed
merge sort for double linked list: done
1 parent 0c79727 commit 1d726b2

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

src/me/ramswaroop/linkedlists/MergeSort.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ public static <E extends Comparable<E>> SingleLinkedNode<E> mergeTwoSortedLists(
104104
curr = curr.next;
105105
}
106106

107+
// return the node next to the dummy node
107108
return head.next;
108109
}
109110

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package me.ramswaroop.linkedlists;
2+
3+
import me.ramswaroop.common.DoubleLinkedList;
4+
import me.ramswaroop.common.DoubleLinkedNode;
5+
6+
/**
7+
* Created by IntelliJ IDEA.
8+
*
9+
* @author: ramswaroop
10+
* @date: 7/7/15
11+
* @time: 4:34 PM
12+
*/
13+
public class MergeSortDoubleLinkedList {
14+
15+
/**
16+
* Merge sort for linked list starting at {@param node}.
17+
*
18+
* @param node
19+
* @param <E>
20+
* @return
21+
*/
22+
public static <E extends Comparable<E>> DoubleLinkedNode<E> mergeSort(DoubleLinkedNode<E> node) {
23+
if (node == null || node.next == null) return node;
24+
25+
DoubleLinkedNode<E> middleNode, head1, head2;
26+
27+
middleNode = divideInTwoHalves(node);
28+
29+
head1 = mergeSort(node);
30+
head2 = mergeSort(middleNode);
31+
32+
return mergeTwoSortedLists(head1, head2);
33+
34+
}
35+
36+
37+
/**
38+
* Divides a linked list starting from {@param node} into 2 halves
39+
* and returns the starting {@code node} of the second half.
40+
*
41+
* @param node
42+
* @param <E>
43+
* @return
44+
*/
45+
public static <E extends Comparable<E>> DoubleLinkedNode<E> divideInTwoHalves(DoubleLinkedNode<E> node) {
46+
DoubleLinkedNode<E> slow = node, fast = node, prev = slow;
47+
48+
if (node == null || node.next == null) {
49+
return null;
50+
}
51+
52+
while (fast != null && fast.next != null) {
53+
prev = slow;
54+
slow = slow.next;
55+
fast = fast.next.next;
56+
}
57+
prev.next = null;
58+
return slow;
59+
}
60+
61+
62+
/**
63+
* Merges two sorted lists starting at {@param node1} and {@param node2}
64+
* into one and returns its starting node.
65+
* <p/>
66+
* This method is similar to {@link MergeTwoSortedLists#mergeTwoSortedLists}
67+
*
68+
* @param node1
69+
* @param node2
70+
* @param <E>
71+
* @return
72+
*/
73+
public static <E extends Comparable<E>> DoubleLinkedNode<E> mergeTwoSortedLists(DoubleLinkedNode<E> node1,
74+
DoubleLinkedNode<E> node2) {
75+
DoubleLinkedNode<E> curr1 = node1, curr2 = node2, head, curr;
76+
77+
if (node1 == null && node2 == null) return null;
78+
79+
head = curr = new DoubleLinkedNode<>(null); // dummy node
80+
81+
while (curr1 != null || curr2 != null) {
82+
// handle cases where either of the list run out first
83+
if (curr1 == null) {
84+
curr.next = curr2;
85+
curr2.prev = curr;
86+
curr2 = curr2.next;
87+
} else if (curr2 == null) {
88+
curr.next = curr1;
89+
curr1.prev = curr;
90+
curr1 = curr1.next;
91+
} else if (curr1.item.compareTo(curr2.item) < 0) { // advance the current pointer of the
92+
// list having smaller {@code item}
93+
curr.next = curr1;
94+
curr1.prev = curr;
95+
curr1 = curr1.next;
96+
} else if (curr1.item.compareTo(curr2.item) > 0) {
97+
curr.next = curr2;
98+
curr2.prev = curr;
99+
curr2 = curr2.next;
100+
} else { // both nodes are equal so add both to the result
101+
curr.next = curr1;
102+
curr = curr.next;
103+
curr1 = curr1.next;
104+
curr1.prev = curr;
105+
curr.next = curr2;
106+
curr2.prev = curr;
107+
curr2 = curr2.next;
108+
}
109+
110+
curr = curr.next;
111+
}
112+
113+
// the dummy node should be unlinked
114+
head.next.prev = null;
115+
116+
// return the node next to the dummy node
117+
return head.next;
118+
}
119+
120+
public static void main(String a[]) {
121+
DoubleLinkedList<Integer> linkedList = new DoubleLinkedList<>();
122+
linkedList.add(21);
123+
linkedList.add(33);
124+
linkedList.add(89);
125+
linkedList.add(21);
126+
linkedList.add(44);
127+
linkedList.add(67);
128+
linkedList.printList();
129+
linkedList.printList(mergeSort(linkedList.head));
130+
}
131+
}

0 commit comments

Comments
 (0)