Author
Please Help! Recursive call
pot tom
Greenhorn
Joined: Apr 27, 2007
Posts: 3
posted Apr 27, 2007 11:31:00
0
Sorry for posting here, badly I need help from expert like you! Can any one explain why my codes does not runs? public static IntNode difference(IntNode head1, IntNode head2) { if(head1==null && head2==null) return null; if(head1==null) return null; if(head2==null) return head1; if(head1.data==head2.data){ IntNode special = difference(head1.next, head2.next); return special; } if(head1.data!=head2.data){ IntNode special = difference(head1.next, head2); head1.next=special; } if (head1.data < head2.data){ IntNode special =difference(head1, head2.next); return special; } return head1; } lets say: head1 lis: 3 -> 6 -> 9 -> 10 head2 list is: 5-> 6-> 10 Answer should be: 3->9, can any one correct me! public static IntNode mergeUnique(IntNode head1, IntNode head2) { if(head1==null && head2==null) return null; if(head1==null) return head2; if(head2==null) return head1; if(head1.data==head2.data){ IntNode special = difference(head1.next, head2.next); return special; } if(head1.data!=head2.data){ IntNode special = difference(head1.next, head2); head1.next=special; } if (head1.data < head2.data){ IntNode special=mergeUnique(head1.next, head2); head1.next=special; return head1; } if (head2.data < head1.data){ IntNode special=mergeUnique(head1, head2.next); head2.next=special; return head2; } return head1; } lets say: head1 lis: 3 -> 6 -> 9 -> 10 head2 list is: 5-> 6-> 10 Answer should be: 3,5,6,9,10, can any one correct me! Note that IntNode class contain: public class IntNode { public int data; public IntNode next; public IntNode() { data = 0; next = null; } public IntNode(int data) { this.data = data; next = null; } public IntNode(int data, IntNode next) { this.data = data; this.next = next; } }
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
posted Apr 27, 2007 12:22:00
0
Welcome to the Ranch. Is this the same problem as you posted on the intermediate forum? Please don't post the same question twice. See what Fred Rosenberger has already told you. All those if something == null blocks look like a fruitful source of errors.
subject: Please Help! Recursive call