MergePoint (LinkList list1, LinkList list2){
p = list1.head;
q = list2.head;
while (p.next!=null && q.next!=null){
if (p.next == q.next){
System.out.print(p.value + " is the Merging node");
return;
}
p=p.next;
q=q.next;
}
}
I’m trying to solve a problem to find out the merging node of 2 linked lists.
Before looking at other solutions, I decided to write my code and then compare to the other existing solutions.
The approach ive taken here is to find the common node that both the list pointers are pointing to.
Do you agree with this code or is there something im missing here?
Your code will only work for the special class of cases where the merging node is in the same position in both the lists. I don’t think there’s an easy, sub-O(n^2) way to do this — in other words, you’ll need to compare every node of one list, with every next of second list, and vice versa.