Technical Interview

Tips, questions, answers and puzzles at
www.technical-interview.com

Sunday, May 2, 2010

Detect merged linked list

Given the pointers to 2 linked list, how will we find out if they are merged? i.e. at some point they point to the same address.

Ans

Pick one of the lists and traverse it from head to tail. For each node encountered, compare it to the head pointer of the other list. If you find a match then the second list is embedded within the first list.

If you don't find a match, then repeat the procedure by traversing the second list.

No comments:

Post a Comment