Skip to content

Commit

Permalink
Added doubly linked list mergesort example
Browse files Browse the repository at this point in the history
  • Loading branch information
pulkit4tech committed Aug 5, 2016
1 parent bda9407 commit 67358aa
Showing 1 changed file with 94 additions and 9 deletions.
103 changes: 94 additions & 9 deletions LinkedListQuestion.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,104 @@ public static void main(String[] args) throws Exception {
void solve() throws Exception {
// reverseLinkedList();
//detect_and_remove_loop();
double_merge_sort();
}

void double_merge_sort(){
dpush(15);
dpush(10);
dpush(5);
dpush(20);
dpush(2);
dpush(3);

push(15);
push(10);
push(5);
push(20);
push(2);
push(3);
dprintList(dhead);
dhead = mergeSortList(dhead);
dprintList(dhead);
}

DoubleListNode dhead;

class DoubleListNode {
int val;
DoubleListNode next;
DoubleListNode prev;

DoubleListNode(int x) {
val = x;
}
}

DoubleListNode mergeSortList(DoubleListNode head) {

if (head == null || head.next == null)
return head;

DoubleListNode second = split(head);

head = mergeSortList(head);
second = mergeSortList(second);

return merge(head,second);
}

DoubleListNode merge(DoubleListNode l, DoubleListNode r) {
if(l == null)
return r;
if(r == null)
return l;

printList(head);
head = mergeSortList(head);
printList(head);
if(l.val < r.val){
l.next = merge(l.next, r);
l.next.prev = l;
l.prev = null;
return l;
}else{
r.next = merge(l, r.next);
r.next.prev = r;
r.prev = null;
return r;
}
}

DoubleListNode split(DoubleListNode head){
DoubleListNode fast = head, slow = head;
while(fast.next != null && fast.next.next != null){
fast = fast.next.next;
slow = slow.next;
}
DoubleListNode temp = slow.next;
slow.next = null;
return temp;
}

void dpush(int data){
DoubleListNode new_node = new DoubleListNode(data);
if(dhead!=null)
dhead.prev = new_node;
new_node.next = dhead;
dhead = new_node;
}

void dprintList(DoubleListNode x) {
if(x != null){
DoubleListNode temp = null;
pout.println("forward:");
while (x != null) {
pout.print(x.val + " ");
temp = x;
x = x.next;
}
pout.println();
pout.println("backward:");
while(temp != null){
pout.print(temp.val + " ");
temp = temp.prev;
}
pout.println();
}
}

ListNode head;

class ListNode {
Expand Down

0 comments on commit 67358aa

Please sign in to comment.