To detect a loop in linked list we have the following options
√-Hash Table ( Elements needs to be unique)
t = O(n)
s=O(n)
√- Modifying Existing Data Structure
t = O(n)
s=O(n)
√-Two Pointer
t = O(n)
s=O(n)
Best Approach- Two Pointer approach (Floyd’s Cycle-Finding Algorithm)
►Traverse linked list using two pointers.
►Move slowpointer(sp) by one and fastpointer(fp) by two.
►If these pointers meet at the same node then there is a loop. If pointers do not meet then linked list doesn’t have a loop.
►Source Code : https://github.com/webtechschool/Data...
Support Us
❤Facebook : / webtechschool.learn
❤Instagram: / webtechschool