This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Please Help! Recursive call Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Reply locked New topic
Author

Please Help! Recursive call

pot tom
Greenhorn

Joined: Apr 27, 2007
Posts: 3
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: 38057
    
  22
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Please Help! Recursive call
 
Similar Threads
Please Help! recursive call
BST problem (Online waiting)
Linked List Operations adding node after intervals.
Weird thing happening in Linked Lists
Creating a linked list with nodes from different list.