java - Recursive merge sort of two Linked Lists using third reference -
i trying solve recursive merge sort using third reference. wanted take third reference , keep on linking nodes 2 linked lists (two linked lists individually sorted) in sorted order without creation of node.
i see there recursive program referenced here. wanted try using third reference keep on messing up. let me know condition missing here?
import java.util.arraylist; import java.util.listiterator; public class mergelinkedlistsintoexisting { public static void main(string[] args){ node nodelist1 = null, nodelist2 = null; node temp = null; arraylist<integer> array1 = new arraylist<integer>(); array1.add(3); array1.add(7); array1.add(9); arraylist<integer> array2 = new arraylist<integer>(); array2.add(1); array2.add(2); array2.add(8); nodelist1 = add(nodelist1, array1); nodelist2 = add(nodelist2, array2); system.out.println("**list 1**"); print(nodelist1); system.out.println("**list 2**"); print(nodelist2); system.out.println("sorted list"); node nodelist3 = mergetwolists(nodelist1, nodelist2, temp); print(nodelist3); } private static node add(node node, arraylist<integer> list){ node current = node; node head = node; listiterator<integer> = list.listiterator(); while(it.hasnext()){ if(head==null){ head = new node(); head.data = it.next(); head.next=null; node = head; } else{ current = new node(); current.data = it.next(); current.next = null; node.next = current; node = node.next; } } return head; } private static void print(node node) { if(node!=null){ while(node.next!=null){ system.out.print(node.data + " "); node = node.next; } system.out.println(node.data); } else{ system.out.println("no elements in linkedlist."); } } private static node mergetwolists(node nodelist1, node nodelist2, node temp) { if(nodelist1 == null) return nodelist2; if(nodelist2 == null) return nodelist1; if(nodelist1.data <= nodelist2.data){ if(temp == null){ temp = nodelist1; temp.next = mergetwolists(nodelist1.next, nodelist2, temp.next); } else{ system.out.println(temp.data); temp.next = mergetwolists(nodelist1.next, nodelist2, temp.next); } }else{ if(temp == null){ temp = nodelist2; system.out.println(temp.data); temp.next = mergetwolists(nodelist1, nodelist2.next, temp.next); } else{ system.out.println(temp.data); temp.next = mergetwolists(nodelist1, nodelist2.next, temp.next); } } return temp; } }
the resolution should temp.next in recursive call of mergetwolists. correct me , approach required.
edit: looked @ again, , think main modification fixed code removing special cases temp equals null.
i modified code , got working. pass through temp
instead of temp.next
, , don't need special case if temp null.
import java.util.arraylist; import java.util.listiterator; public class mergelinkedlistsintoexisting { public static void main(string[] args){ node nodelist1 = null, nodelist2 = null; node temp = null; arraylist<integer> array1 = new arraylist<integer>(); array1.add(3); array1.add(7); array1.add(9); arraylist<integer> array2 = new arraylist<integer>(); array2.add(1); array2.add(2); array2.add(8); nodelist1 = add(nodelist1, array1); nodelist2 = add(nodelist2, array2); system.out.println("**list 1**"); print(nodelist1); system.out.println("**list 2**"); print(nodelist2); system.out.println("sorted list"); node nodelist3 = mergetwolists(nodelist1, nodelist2, temp); print(nodelist3); } private static node add(node node, arraylist<integer> list){ node current = node; node head = node; listiterator<integer> = list.listiterator(); while(it.hasnext()){ if(head==null){ head = new node(); head.data = it.next(); head.next=null; node = head; } else{ current = new node(); current.data = it.next(); current.next = null; node.next = current; node = node.next; } } return head; } private static void print(node node) { if(node!=null){ while(node.next!=null){ system.out.print(node.data + " "); node = node.next; } system.out.println(node.data); } else{ system.out.println("no elements in linkedlist."); } } private static node mergetwolists(node nodelist1, node nodelist2, node temp) { if(nodelist1 == null) return nodelist2; if(nodelist2 == null) return nodelist1; if(nodelist1.data <= nodelist2.data){ temp = nodelist1; temp.next = mergetwolists(nodelist1.next, nodelist2, temp); }else{ temp = nodelist2; temp.next = mergetwolists(nodelist1, nodelist2.next, temp); } return temp; } } public class node{ int data; node next; }
Comments
Post a Comment