retagged by
1,630 views
2 votes
2 votes
Given two singly linked list of size n. The time complexity of computing the union and intersection of two linked list is.

a) O(n) and O(nlogn)

b) O(n) and O(n)

c) O(nlogn) and O(nlogn)

d) O(nlogn) and O(n)
retagged by

1 Answer

Best answer
2 votes
2 votes
Should be O(n).

For union :

Take all elements of the first list and simultaneously construct a hashtable for first list. This takes O(n) time.

For 2nd list, probe elements in the hash table first. If there is collision, don't put it in the union list, else put it. This takes O(n) time. Hence net time : O(n).

For intersection :

Construct hastable for first list. This will take O(n) time.

For 2nd list, probe elements in the hash table first. If collision happens, put it in intersection list. This also takes O(n) time. Hence net time : O(n)
selected by

Related questions

1 votes
1 votes
1 answer
1
5 votes
5 votes
1 answer
2
hacker16 asked Nov 14, 2017
3,458 views
What would be the asymptotic time complexity to add a node at the end of singly linked list, if the pointer is initially pointing to the head of the list?O(n)O(1)θ(n2)θ...
1 votes
1 votes
3 answers
3
Angkit asked Apr 23, 2017
14,529 views
Which sorting algorithm can be used to sort a random linked list with minimum time complexity ?A)mergesortB)quicksortC)radixsortD)insertionsortE)heapsort
2 votes
2 votes
1 answer
4