Finding the intersecting node from two intersecting linked lists

This takes O(M+N) time and O(1) space, where M and N are the total length of the linked lists. Maybe inefficient if the common part is very long (i.e. M,N >> m,n)

  1. Traverse the two linked list to find M and N.
  2. Get back to the heads, then traverse |M − N| nodes on the longer list.
  3. Now walk in lock step and compare the nodes until you found the common ones.

Edit: See more here.

Leave a Comment