I'm assuming that we are talking about simple linked lists and we can safely create a hash table of the list element pointers.
Q1: Iterate to end of both lists, If the respective last elements are the same, the lists merge at some point.
Complexity - O(N)
, space complexity - O(1)
Q2:
- Put all elements of one list into a hash table
- Iterate over 2nd list, probing the hash table for each element of the list. The first hit (if any) is the merge point, and we have the position in the 2nd list.
- To get the position in the 1st list, iterate over the first list again looking for the element found in the previous step.
Time complexity - O(N)
. Space complexity - O(N)
Q3:
- As Q1, but also reverse the direction of the list pointers.
- Then iterate the reversed lists looking for the last common element - that is the merge point - and restoring the list to the original order.
Time complexity - O(N)
. Space complexity - O(1)