26. Linked List-3 : (1 singly LL Problems)
Medium Problems LL-
- find middle node✅ (2 pointer/ tortoise hare)
- delete middle node✅
- delete nth node from back✅
- detect a loop✅ (hashing/ tortoise hare)
- length of loop✅ (hashing/ tortoise hare)
- find starting point of loop✅🔥 (hashing/ tortoise hare)
- reverse LL✅ (stack, 2 pointer, recursion)
- check palindrome✅ (stack, half reversal)
- add 1 to number represented by LL✅ (stack, reversal, recursion)
- segregate odd even numbered nodes✅(Link Changing)
- sort LL of 0s, 1s, 2s✅ (Link Changing)
dummy node mein desired nodes jodte jao aur end mein dummy ke aage se bani hui poori new linked list dedo
1. finding Middle of LL-
Brute- O(n): length find krke middle elmt ki posn calculate krlo & then traverse krlo us tak...
Optimal O(n/2): ( Tortoise-Hare approach )
use 2 pointers for traversal- fast pointer moves by 2 steps & slow pointer by 1 step
when fast ptr reaches end of LL, at the same time slow ptr can be found at middle element
write while loop condition as per fast being last or 2nd last node
----------------------------------------------------------------------------------------------------------------------------
2. deleting Middle of LL-
Let, list has L nodes:
Brute: nth node from back means (L-n+1) th node from front so ques reduces to del kth node
Optimal-
- When fast is n nodes ahead, & both move together
- After L-n steps, fast reaches the last node
- And slow is at position ( L-n ) exactly before the nth node from end.
4. Reverse LL-
-----------------------------------------------------------------------------------------------------------------------------
Brute force-
use a stack
------------------------------------------------------
Better-
reverse LL
---------------------------------------------
Optimal-
instead of reversing we will use recursion to move back
Brute force-
we can use a vector to store odd and then even numbered nodes and then over write the actual nodes using this list
Optimal-
without using extra space-
1. we will connect all odd nodes
2. we will connect all even nodes
3. we will connect last odd node to first even node
at last, even will always be ahead of odd & even will be at last node of LL
so now, connect odd end to even start, which we stored in an extra ptr at start
Brute- time: O(2n)
count no. of 0s, 1s and 2s and over write the LL
Optimal- time: O(n)
Link changing by connect all 0s separately then all 1s separately then all 2s separately then connect end of 0s to start of 1s and end of 1s to start of 2s
Brute Force-
use hashing
Optimal- (use tortoise and hare algo)
a time will come when both will meet
Optimal-
Tortoise hare algo se pehle toh detect loop wale code se slow fast ko match karake loop ke andar kisi ek node pe laao, ab kyuki loop ke andar hi ghumta rahega vo toh fast wale ko ek ek badhake count krte rho jab tak vo slow ko na mil jaye toh loop ke total nodes count ho jayenge-----------------------------------------------------------------------------------------------------------------------------
Brute-
use hashing
Optimal-🔥🔥🔥
use tortoise hare algo
1. detect loop wale code se collision point pe fast ptr ko le jao
2. ab slow ptr ko LL ke start pe le jaake , slow aur fast ko 1 se aage badhate rho
3. the node at which they collide is start of loop
Intuition-
as per relative velocity, fast covers 'd' dist & slow covers 0 dist
or, we can also say, fast covers '2d' distance & slow covers 'd' dist
at each step, distance 'd' between them reduces by 1
we can also say dist between slow/ start of loop to collision point is 'd'
so they will collide in this problem since both covers L1 distance


Comments
Post a Comment