Why not this approach?

  • 0

    The second approach(HashSet) has a inherent flaw to it, i.e. if there are two linked lists such as L1 (15, 5, 25, 30. 20) and L2 (25, 40, 45,30,20) . Let's say, L1 is stored in the HashSet. Now, if every element of L2 is checked against its presence in the hashSet, we return 25 as the intersection point while in reality, there isn't any that exists. So, one fail proof way of solving this problem can be that we can reverse both the linked lists and then traverse them till a point where you see two different elements. Reversing a linked list would take O(n) time and O(theta)(n) space complexity. Traversing the linked lists again would take O(n) time complexity. So, in total it would take O(n) time. Could anyone please point out the drawbacks of this approach?

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.