I am addicted to go through the procedure of brute to optimize solution and sometime its frustrating when someone just mug up the solution without giving insights like you striver.
@AshishSingh-he2qo3 ай бұрын
Solved with optimal approach without seeing the solution ❤❤🎉
@endlesslearning263 ай бұрын
i solved via brute force but couldn't think for the optimal solution sadly
@KARTHIKEYANR-b1nАй бұрын
I solved using brute
@MaheshPatil-of1zy4 ай бұрын
Solved By Own by watching your previous Lecture.
@Benstokes44444 ай бұрын
optimized wala ya brute force
@shamanthhegde3 ай бұрын
Let me know pls whether you did the same or something different //Time Complexity: O(n) //Space Complexity: O(1) public class Solution { public int lengthOfCycle(ListNode head) { if(head == null || head.next == null) return 0; int count = 0; ListNode fast = head; ListNode slow = head; while(fast!=null && fast.next!=null) { fast = fast.next.next; slow = slow.next; count++; if(fast == slow) { return count; } } return 0; } }
@ritikshandilya70757 ай бұрын
Immense approach for any beginner , its crisp as well as detailed . Thanks Striver for being Striver
@stith_pragya6 ай бұрын
Understood.......Thank You So Much for this wonderful video........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@_CodeLifeChronicles_5 ай бұрын
the best tutorials ever
@ArpitDharmshaktu4 ай бұрын
i think we can decrease the time cmplx. a little bit more by just taking the length from starting to the coliision node i.e (L1+d) where L1 is the distance from starting to the slow node when it reaches the junction and d is the distance of that slow node to the coliision of nodes for better understanding of how L1+d can be the length of the loop check the last video which is finding the starting point of loop.
@sandiprath48963 ай бұрын
Yes, the same thought also comes to mind, but I don't know why it's not working.
@AkOp-bf9vm3 ай бұрын
it will not work for LL 1->2->3->4->5->6->7->8->9->8 here length of Loop is 2 but slow will be more than 2. THIS CODE WILL ONLY WORK WHEN SLOW POINTER REACH STARTING NODE OF THE LOOP AND FAST POINTER SHOULD NOT PASS THE CURRENT NODE OF SLOW
@vamsikrishnagannamaneni91216 күн бұрын
distance from slow node to collision of nodes is infact 'd' but how will find it? After finding the starting point of cycle will you traverse the slow again till collision? This will yield same time complexity
@hareshnayak73027 ай бұрын
Understood,thanks striver for this amazing video.
@VisibleNasir9 ай бұрын
Thank you 💟 🙃 I'm doing dsa with striver ! But sorry because I downloaded all Videos 😅
@satyamgupta-pv2ib3 ай бұрын
cnt not go at 10:02 slow =fast node at the last , cnt start with one cnt stop at one step back where slow=fast node ......code is write But explaination is awersome Thankyou Striver
@SouravSahu-f1y7 ай бұрын
Sir really grateful to you 🙏 just one question if the heap videos will come? As not liking any other tutor after learning from you
@BadalDesai-s7h9 ай бұрын
Hey Striver just wondering, In the last video about finding the loop starting point you said that the loop length will be "L1 + d" so can't we just return the number of steps slow moved before we detect the loop (fast==slow) ??
@pravinthakur77917 ай бұрын
int lengthOfLoop(Node *head) { // Write your code here Node* slow = head; Node* fast = head; while(fast != nullptr && fast->next != nullptr) { slow = slow->next; fast = fast->next->next; if(slow == fast) { coutnext; } cout
@shamanthhegde3 ай бұрын
We can simplify it actually Just do the old approach of detect cycle... and whenever the fast and slow meets...the distance travelled by the slow pointer becomes the size of the loop //Time Complexity: O(n) //Space Complexity: O(1) public class Solution { public int lengthOfCycle(ListNode head) { if(head == null || head.next == null) return 0; int count = 0; ListNode fast = head; ListNode slow = head; while(fast!=null && fast.next!=null) { fast = fast.next.next; slow = slow.next; count++; if(fast == slow) { return count; } } return 0; } }
@YourCodeVerse10 ай бұрын
Understood✅🔥🔥
@NazeerBashaShaik6 ай бұрын
Understood, thank you.
@akshaysmart62573 ай бұрын
So far After watching these 15 lectures on LinkedList why I feel more confident in LinkedList than Arrays😅😅?? Is the linkedlist concept itself easy or I didn't concentrate more on Arrays. I even completed the Arrays playlist 2 times😒
@ManishLakkavatri11 ай бұрын
Understood! but what will be the Time Complexity??
@govindasharma907711 ай бұрын
Time complexity will be O(N*length of loop) but we can consider it somewhere around O(N) and space will be O(1)
@IITians10 ай бұрын
@@govindasharma9077 TC: O(N + length of loop)
@abhaymandal49039 ай бұрын
O(2n)
@abhaymandal49039 ай бұрын
@@govindasharma9077 it will not (n*length of loop ) as for every value of n there is not seperate loop. There is linear relation . So it will be O(2n)
@2amCoder9 ай бұрын
O(len)+O(len of loop)
@krishkothari86346 ай бұрын
int lengthOfLoop(Node *head) { // Write your code here Node* slow = head; Node* fast = head; while(fast != NULL && fast->next != NULL) { slow = slow->next; fast = fast->next->next; if(slow == fast) { int cnt = 0; do { fast = fast->next; cnt++; } while(fast != slow); return cnt; } } return 0; }
@NonameNoname-f2t8 ай бұрын
superb
@selene87217 ай бұрын
Thank you so much!!
@RajNamdev_193 ай бұрын
Understood;
@ಪ್ರಯತ್ನ-ಞ3ಧ2 ай бұрын
7:05 superbike sound 😌.... Yes guys I got distracted..
@striverdaaadi10 ай бұрын
awesome bro
@TS-oj3vd7 күн бұрын
time complexity?
@dewanandkumar85896 ай бұрын
Understood
@abhinavprabhat441811 ай бұрын
int lengthOfLoop(Node *head) { Node* fast = head; Node* slow = head; int cnt = 0; while (fast != NULL && fast->next != NULL) { slow = slow->next; fast = fast->next->next; if (slow == fast) { do { cnt++; fast = fast->next; } while (slow != fast); return cnt; } } return 0; }
@nishant45952 ай бұрын
was able to solve this.....
@iamnottech89189 ай бұрын
complexity ?
@priyanshuchaudhary64696 ай бұрын
i have a question,in your last videos you have taught us that the total distance of loop will be d+L1 where L1 will be the same distance from the head to that node which is the starting node of the loop and d is the distance from the starting node of the loop where slow was present and when slow pointer meets with fast pointer in the clockwise direction. So why can't we only calculate the total distance from starting to the node where slow pointer meets the fast pointer int count=0; Node slow=head,fast=head; while(fast!=null && fast.next!==null) { slow=slow.next; fast=fast.next.next; count++; if(slow==fast) { return count; } } i have just written main code Someone plz explain
@priyadarsimishra79096 ай бұрын
You need to account for one edge case where the length of the loop is not necessarily always L1 + d. This is because L1 could be large and the loop could be small. The initial L1 distance is always divisible to the length of the loop (you can just draw some cases out and see this).
@shamanthhegde3 ай бұрын
@@priyadarsimishra7909can you give me a test case because as far i see even for the case you mentioned it seems to work fine
@priyadarsimishra79093 ай бұрын
@@shamanthhegde I am saying that it is not always L1 + d because the length of the loop could be only 2 or 3 nodes whereas L1 is like 5 nodes, so in that case you just loop till fast reaches itself again. That would be the loop length else if fast reaches slow then we have the loop length as well.
@AkOp-bf9vm3 ай бұрын
@@shamanthhegde it will not work for LL 1->2->3->4->5->6->7->8->9->8 here length of Loop is 2 but slow will be more than 2. THIS CODE WILL ONLY WORK WHEN SLOW POINTER REACH STARTING NODE OF THE LOOP AND FAST POINTER SHOULD NOT PASS THE CURRENT NODE OF SLOW
@Trollvideos115411 ай бұрын
nice dude
@utkarshiitbhu42045 ай бұрын
but when they collide suppose from starting of loop their length is d and the remaining is L1 which is length from starting link list to starting loop so loop is L1+D whish is the amount slow pointer covers???
@shamanthhegde28203 ай бұрын
Crct bro I too applied the same logic and it works
@AkOp-bf9vm3 ай бұрын
WRONG BRO
@shamanthhegde28203 ай бұрын
@@AkOp-bf9vm why?
@AkOp-bf9vm3 ай бұрын
@@shamanthhegde2820 check for the test case 1->2->3->4->5->6->7->8->9->8 HERE LENGTH OF LOOP IS 2 BUT LENGTH OF SLOW POINTER MOVES FROM START IS GREATER THAN 2
@PixelPioneer9280011 ай бұрын
understood
@AK-wc1cv5 ай бұрын
Can someone please explain why he wrote fast=fast.next before and inside while loop
@madhu_mohanreddy4 ай бұрын
before to move the fast to next node and next in the while loop to start the count from there!!
@PremShinde-p4q7 ай бұрын
What will be time complexity of optimal approach ? Will it be O(2N)? Plz answer
@shaiksoofi37414 ай бұрын
understoodd
@SamyakSharma-oy1bv16 күн бұрын
respect++;
@rajatshukla2605Ай бұрын
Undestood
@successvibes160411 ай бұрын
int lengthOfLoop(Node *head) { Node* slow = head; Node* fast = head; while(fast!=NULL and fast->next!=NULL){ fast = fast->next->next; slow = slow->next; if(fast == slow){ int ct = 1; slow = head; //slow = slow->next; while(slow != fast){ slow = slow->next; ct++; } return ct; } } return NULL; why this code is not working as distance from head to the point where fast and slow collide show be equal to the length of loop because in last lec where we calculated the starting point of loop we have used this concept slow = head than move till fast collide with slow someone plz explain
@successvibes160411 ай бұрын
someone plz answer 🤔🤔
@GauravSharma-ij1ym9 ай бұрын
@@successvibes1604 just take a Linked List with just 3 elements in loop and atleast 8 elements in linear chain before loop . Dry run it and u will get your amswer
@aarzookhunger94878 ай бұрын
it shouldn't be calculated from slow = head *after you initialised the ct* instead do slow = slow->next as in the loop that we've detected , it will move slow one step ahead than fast and calculate the length of loop until we again hit fast. Else the code looks correct.
@ashishpradhan62505 ай бұрын
arigato
@rinokamura115210 ай бұрын
Can someone send brute force method 's code .
@vineetverma396410 ай бұрын
int findLoopLength(ListNode* head) { std::unordered_map nodeMap; // Map to store node addresses and their corresponding indices ListNode* current = head; int index = 0; while (current != nullptr) { // Check if the current node is already in the map if (nodeMap.find(current) != nodeMap.end()) { // Loop detected, calculate the length int loopStartIndex = nodeMap[current]; return index - loopStartIndex; } // Add the current node to the map nodeMap[current] = index; // Move to the next node current = current->next; index++; } // No loop found return 0; }
@rinokamura115210 ай бұрын
@@vineetverma3964 Love ya bruh
@iamnoob75938 ай бұрын
US
@cenacr0076 ай бұрын
us
@dayashankarlakhotia4943 Жыл бұрын
public int countNodeInloop{ Node head){ Node fast =head,slow=head; int cnt=1; while(fast.next!=null&&fast.next.next!=null){ slow=slow.next; fast=fast.next.next; if(slow==fast){ fast=fast.next; while(slow!=fast){ cnt++; fast =fast.next; } return cnt; } } return 0; }