54. DLL-2

1. reverse DLL
2. delete given element occurrences in dll
3. find pairs with given sum in sorted dll
4. remove duplicates from sorted dll
5. 
---------------------------------------------------------------------------------------------------------------
1. Reverse a DLL-
Brute-
use stack and do data over write

Better-
use start and end pointer and swap values doing (start  = start-->next) & (end = end-->prev)

Optimal-⭐⭐
changing links instead of over writing data 





---------------------------------------------------------------------------------------------------------------

2. Delete all occurrences of key in DLL-

------------------------------------------------------------------------------------------------------------

3. Find pairs with given sum in sorted DLL-

Brute- (similar to brute force of subarray traversal)

temp1 rakho start pe aur temp2 ko tab tak traverse karao from start+1 till jaha tak 
temp2 + temp1 <= k
fir temp1 ko start+1 pe rakho and firse temp2 chalao
and repeat this for all nodes being temp1 in an iteration
--------------------------------------------------
Optimal-
use 2 pointer
---------------------------------------------------------------------------------------------------------------

4. Remove duplicates from sorted DLL-

------------------------------------------------------------------------------------------------------------





Comments

Popular posts from this blog

18. greedy algorithm

19. strings (LB)

17. BINARY SEARCH on 2d arrrays