Create a hash table.
Go through the first list of links, mark the entries when you visit them. NA) Go through the second list of links, mark the entries (another flag, etc.) when you visit them. O (M)
Move the hash table and find all records with one LL member. Create new LL members when you find the entries. HE)
Total difficulty: O (N) + O (M) + O (Max (N, H, M)) => O (N)
Note. Edited answer for Saurabha.
source share