Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
    Bookmark Topic Watch Topic
  • New Topic

Please Help! Recursive call

 
pot tom
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Marshal
Posts: 52636
119
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
Get off me! Here, read this tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!