I have an assignment where I need to find the intersections of 2 singly-linked (singly vs singly) lists. I also have to do it for 2 doubly linked (doubly vs doubly) lists:
For singly linked list, I use
mergeSort()
to sort both lists and then compare item by item ==> O(m.log(m) + n.log(n))I am confused for doubly linked lists: the same could work, but I guess there might be a faster way.
Can someone please tell me if there is a more efficient way to find the intersection of the 2 doubly linked lists? I thought maybe we could merge them and check for duplicates, but I am not sure how that would work and how efficient it would be.
Edit: Note that 'intersection' here means a shared value, not a common node. Edit: the implementation should be done without the use of hashes