write a recursive method which takes two linked lists as arguments and (without any loops) returns an intersection of the two lists. I'm baffled! Here are two versions of code I came up with....please help
IntNode commonNodes = new IntNode();
IntNode ptr = commonNodes;
IntNode temp2 = head2;
IntNode temp = head1;
if (temp.next == null) {
if (head1.next != null) {
if (head1.data == temp2.data) {
commonNodes.data = temp.data;
commonNodes = commonNodes.next;
}
commonNodes.next = common(head1.next, head2);
}
}
if (temp2.next != null) {
commonNodes.next = common(head1, temp2.next);
if (head1.data == temp2.data) {
commonNodes.data = temp.data;
commonNodes = commonNodes.next;
}
}
return commonNodes;
public static IntNode common(IntNode head1, IntNode head2)
{
IntNode a = head1;
IntNode b = head2;
IntNode commonNodes = new IntNode();
IntNode ptr = commonNodes;
if (a.next != null && b.next != null) {
if (a.data == b.data) {
ptr.data = a.data;
ptr = ptr.next;
a = a.next;
b = b.next;
}
else if (a.data < b.data) {
a=a.next;
}
else {
b=b.next;
}
}
return commonNodes;
}

New Topic/Question
Reply




MultiQuote



|