U
user923005
Ravishankar S said:
<snip>
Indeed. Consider these linked lists:
listA: A -- B -- C -- D
\
E -- F -- G
/
listB: H -- I
You compare A with H, B with I, C with E, D with F, E with G. Now the loop
stops, and q is NULL. You missed the merge point completely.
I believe this has to be done in two nested loops (or at least, "as if" it
were two nested loops! - i.e. O(listAnodecount * listBnodecount)), but I'd
be delighted to be proved wrong.
Consider:
listA: A -- B -- C -- D J -- K
\ /
E -- F -- G
/ \
listB: H -- I |
\ L
N -- M -- /
Or even:
listA: C -- D J -- K
\ /
E -- F -- G
/ \
listB: H -- I L
Graph problems have a nasty habit of turning out harder than they
appear at first glance.
If they can itersect, then they can probably also diverge and cycle.
If you don't test for it, funny things can happen, but not "ha-ha"
funny.