Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Please Help! Recursive call

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
}
}
 
Marshal
Posts: 76798
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Popeye has his spinach. I have this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic