Detect and Remove Loop in Linked List || C++ || By Mohit Gupta

  Рет қаралды 4,509

PhysicsMania-Mohit Gupta [IIT BHU]

PhysicsMania-Mohit Gupta [IIT BHU]

3 жыл бұрын

Follow me on Instagram : / mohitgupta8685
.................. In this video, we discuss how to detect and remove Loop in a Linked List.
-----------------------------------------------------------------------------------------------------------------
iBytes Academy is a leading platform to learn coding.We have courses ranging from C++ with data structures to machine Learning using Python.
------------------------------------------------------------------------------------------------------------------
Explore our programming courses: ibytesacademy.com
-----------------------------------------------------------------------------------------------------------------
Follow us on social media:
Instagram: / ibytes_academy
Facebook: bit.ly/3o9UiMd

Пікірлер: 33
@mohitguptaiitbhu
@mohitguptaiitbhu 3 жыл бұрын
For full course on C++ with data structures and Algorithms,visit ibytesacademy.com/courses/data-structures-and-algorithms/
@anckoor
@anckoor 2 жыл бұрын
excellent
@akashskumar99
@akashskumar99 2 жыл бұрын
nicely done
@DeepakVerma-bc7kc
@DeepakVerma-bc7kc 3 жыл бұрын
When the list of the last node is referencing to first node and rest of cases as well , we can handle like this isLoopDetected =true; if(isLoopDetected) private void removeLoop(Node slow, Node head) { Node curr = head; if (slow == head) { while (slow.next != curr) { slow = slow.next; } } else { while (slow.next != curr.next) { slow = slow.next; curr = curr.next; } } slow.next = null; } If you are using C or C++, use -> instead of dot.
@jayanthk795
@jayanthk795 3 жыл бұрын
How to solve following case? If we have a list with 5 nodes and loop is from last node to first or head node. Then by detect function slow points to head ,So both arguments passed to remove function becomes head and head->next becomes NULL but we need last node ->next to become NULL.
@mohitguptaiitbhu
@mohitguptaiitbhu 3 жыл бұрын
Yes,if our complete list itself is a loop,this wont work because both slow and fast will always meet at head itself.So we can handle this case separately by adding another check. But here,we are solving for the case when it includes a loop and not itself as a whole is loop.But nice point though👍
@rajatthakur3908
@rajatthakur3908 3 жыл бұрын
nicely explained
@mohitguptaiitbhu
@mohitguptaiitbhu 3 жыл бұрын
Thanks
@divyamgupta6415
@divyamgupta6415 3 жыл бұрын
Sir diagram se achche se smjhme aaya. Good explanation sir.
@mohitguptaiitbhu
@mohitguptaiitbhu 3 жыл бұрын
Great👍
@saikiran-xh5lt
@saikiran-xh5lt 3 жыл бұрын
case :1) The above code is not useful in case of loop is happened at head node. After applying the above concept head node next point to null. case: 2) consider this case, "1->2->3->4->1", if loop is detected at node '4' then in this case we end up in infinite loop if we follow above case to remove the loop. we need to consider above two cases.Below is complete code which covers above cases. void detectAndRemoveLoop(Node* head) { if(!head || !(head->next)) return; auto slow = head, fast = head; while(fast && fast->next){ slow = slow->next; fast = fast->next->next; if(slow == fast){ //case 2: if(slow->next == head){ slow->next = nullptr; return; } // case 1: slow = head; if(slow == fast) { while(fast->next != slow) fast = fast->next; } else { while (slow->next != fast->next) { slow = slow->next; fast = fast->next; } } /* since fast->next is the looping point */ fast->next = NULL; /* remove loop */ } } } Let me know your thoughts.
@bill.ionaireproject
@bill.ionaireproject 3 жыл бұрын
Wow great topic sir
@mohitguptaiitbhu
@mohitguptaiitbhu 3 жыл бұрын
thanks :)
@NANDKISHOR-nc1sy
@NANDKISHOR-nc1sy 3 жыл бұрын
Wow quite well explanation 🔥
@mohitguptaiitbhu
@mohitguptaiitbhu 3 жыл бұрын
Thanks :)
@Jyotikumari-zu6dg
@Jyotikumari-zu6dg 3 жыл бұрын
Amazing.. well explained sir.
@mohitguptaiitbhu
@mohitguptaiitbhu 3 жыл бұрын
Thanks,happy coding :)
@sehejwahla5437
@sehejwahla5437 3 жыл бұрын
Great explanation bro. Thanks !!
@mohitguptaiitbhu
@mohitguptaiitbhu 3 жыл бұрын
You are welcome :)
@SURAJKUMAR-ej6ez
@SURAJKUMAR-ej6ez 3 жыл бұрын
Grate Explanation sir. I think in one textcase (while passing slow and head it may possible head and slow both pointing the same node)it will give wrong result. Except this case all are perfectly fine.
@mohitguptaiitbhu
@mohitguptaiitbhu 3 жыл бұрын
This will happen only when our whole list itself is a loop,but in this problem we are assuming that list contains a loop and not whole list is a loop.
@watchlistsclips3196
@watchlistsclips3196 3 жыл бұрын
Good video.You got yourself an extra subscriber
@bhoomikathethwar244
@bhoomikathethwar244 3 жыл бұрын
Woww. Nicely explained ..so proud of you 😊
@vikrantgupta3226
@vikrantgupta3226 3 жыл бұрын
Well explained
@mohitguptaiitbhu
@mohitguptaiitbhu 3 жыл бұрын
Thanks
@sayantikachatterjee1287
@sayantikachatterjee1287 2 жыл бұрын
can anyone please let me know the time complexity here. the first loop takes O(N) but the what about the other two loops?
@ashutoshbichare
@ashutoshbichare 3 жыл бұрын
Bro in removeLoop() function you are writing head=head->next so without taking value of head in some temp variable and then traversing it you are directly started traversing with head so isn't it change the value of head means our linked list will start from 4 and end on 9🤔
@mohitguptaiitbhu
@mohitguptaiitbhu 3 жыл бұрын
It is actually copy of the head pointer that is being used to traverse the linked list.Will soon post a video on pass by value and pass by reference,which will make it clear.
@ashutoshbichare
@ashutoshbichare 3 жыл бұрын
@@mohitguptaiitbhu okk thank you sir🔥
@rohitgpt7201
@rohitgpt7201 3 жыл бұрын
can someone help me understand the case when the loop starts from the starting node itself.. it would really help me to thoroughly understand this problem... :)
@mohitguptaiitbhu
@mohitguptaiitbhu 3 жыл бұрын
If our complete list itself is a loop,same code wont work because both slow and fast will always meet at head itself.So we can handle this case separately by adding another check. But here,we are solving for the case when it includes a loop and not itself as a whole is loop.
@rohitgpt7201
@rohitgpt7201 3 жыл бұрын
@@mohitguptaiitbhu okayy thanks!
L14. Detect a loop or cycle in LinkedList | With proof and Intuition
20:26
Remove Loop from Linked List | C++ | Java | Python
15:58
Ayushi Sharma
Рет қаралды 9 М.
Best Toilet Gadgets and #Hacks you must try!!💩💩
00:49
Poly Holy Yow
Рет қаралды 22 МЛН
Alex hid in the closet #shorts
00:14
Mihdens
Рет қаралды 19 МЛН
Box jumping challenge, who stepped on the trap? #FunnyFamily #PartyGames
00:31
Family Games Media
Рет қаралды 19 МЛН
Detect loop in linked list(floyd algo / Tortoise and hare algo)
9:29
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 111 М.
Understanding and implementing a Hash Table (in C)
24:54
Jacob Sorber
Рет қаралды 347 М.
Whiteboard Coding Interviews: 6 Steps to Solve Any Problem
15:18
Fullstack Academy
Рет қаралды 364 М.
Interview Question: Start of Loop in a Linked List
10:37
Gaurav Sen
Рет қаралды 143 М.
CONST in C++
12:54
The Cherno
Рет қаралды 399 М.
Intersection of Two Linked List | EP 18
14:08
Fraz
Рет қаралды 18 М.
How To Find Loop In Linked List
8:25
CppNuts
Рет қаралды 3,3 М.
Best Toilet Gadgets and #Hacks you must try!!💩💩
00:49
Poly Holy Yow
Рет қаралды 22 МЛН