Amazon Interview Question

Given two linked lists, find out IF they intersect.

Interview Answers

Anonymous

Feb 8, 2011

Solution 1: Traverse the first linked list and mark visited nodes. Traverse 2nd linked list of, the moment we obtain a visited node that's the intersection point. solution2: If size of 2 linked list are different then traverse the bigger linked list to that extent where the remaining length would be same as the smaller one. Then start traversing both the list together. The moment comment address location obtained, that intersection point.

2

Anonymous

Mar 1, 2011

HashMap map = new HashMap(); for (Integer i : list1) map.put(i, i); for (Integer j : list2) { if (map.containsKey(j)) return true } return false;

Anonymous

Feb 7, 2011

Traverse both the linked lists and check if their last elements are the same (address of the elements).

1