Posts

Showing posts from July, 2025

55. LL- hard

Image
1. Flatten LL✅ 2. Rotate LL✅ 3. reverse LL in groups of size K✅ 4. Clone LL with random pointer & next pointer✅ 5. Design Browser History✅ 6. LRU cache✅ 7. LFU cache🔥✅ ----------------------------------------------------------------------------------------------------------------------------- 1. Reverse in groups of K step-1: put temp at head step-2: figure out kth node step-3: point kth node's next to null but before it preserve the next node step-4: reverse this group of LL step-5: do , head = kth node & prvNode = temp  step-6: do, temp = next node (which we preserved in step 3)  step-7: repeat from step 2 step-8: if for a group there are < k elements, then attach all elements of such group starting from temp to the previous node. ----------------------------------------------------------------------------------------------------------------------------- 2. Rotate a LL -----------------------------------------------------------------------------------------------...

54. DLL-2

Image
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 an...

53: Doubly Linked List-1

Image
Representation:- ----------------------------------------------------------------------------------------------------------------------------- Converting array to doubly LL:- ---------------------------------------------------------------------------------------------------------------------------- Deletions in DLL:- 1. Deleting head- 2. deleting tail- 3. deleting Kth node-⭐⭐ 4. Delete given node- ---------------------------------------------------------------------------------------------------------------------------- Insertions in DLL:- 1. before head- 2. before tail- 3. before Kth node- 4. before given node- -----------------------------------------------------------------------------------------------------------------------------

52. Linked-List-4: (Two singly LL problems)

Image
Add 2 LL✅🔥 Poi of 2 LL✅ merge 2 sorted LL✅ merge K sorted LL✅ sort LL🔥 --------------------------------------------------------------------------------------------------------------- 1. Add 2 LL use concept of Dummy node to create a new LL while traversing given 2 LLs --------------------------------------------------------------------------------------------------------------- 2. Poi of 2 LL Brute force-  traverse one LL and hash the nodes & when u traverse the other LL, the first visited node u visit is ans -------------------------------------------------- Better- instead of using extra space, we will move ptr of Longer LL till it reaches starting posn of shorter LL, and then we start simultaneous traversal of both --------------------------------------------------- Optimal- 1. move both ptrs starting from respective heads 2. whenver a ptr reaches last node, move that ptr to head of other linked list & move other ptr as usual 3. now keep moving the ptrs & if u see ...