# Recursion, Linked Lists, and a Lost Soul

Page 1 of 1

## 1 Replies - 971 Views - Last Post: 24 April 2010 - 12:50 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=169886&amp;s=5cbdef396c2bb0ff589ae55eea14adac&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: 0
• Posts: 52
• Joined: 04-April 09

# Recursion, Linked Lists, and a Lost Soul

Posted 23 April 2010 - 07:00 PM

Okay, so I've spend the last two hours trying to figure this one out:
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;
if (temp.next == null) {
commonNodes.data = temp.data;
commonNodes = commonNodes.next;
}
}
}
if (temp2.next != null) {
commonNodes.data = temp.data;
commonNodes = commonNodes.next;
}
}

return commonNodes;

```

```	public static IntNode common(IntNode head1, IntNode 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;

}

```

Is This A Good Question/Topic? 0

## Replies To: Recursion, Linked Lists, and a Lost Soul

Reputation: 0
• Posts: 52
• Joined: 04-April 09

## Re: Recursion, Linked Lists, and a Lost Soul

Posted 24 April 2010 - 12:50 AM

well, this is where the method stands now, I have made progress despite it not being correct. I could really use a little help
```	public static IntNode common(IntNode head1, IntNode head2)
{
IntNode commonNodes = new IntNode();
System.out.print("             returned from here                ");
return commonNodes;
}
}
}
IntNode ptr = commonNodes;
while (ptr.next != null) {
ptr = ptr.next;
}
IntNode n = new IntNode();
ptr.next = n;
print(commonNodes);