- Популярные видео
- Авто
- Видео-блоги
- ДТП, аварии
- Для маленьких
- Еда, напитки
- Животные
- Закон и право
- Знаменитости
- Игры
- Искусство
- Комедии
- Красота, мода
- Кулинария, рецепты
- Люди
- Мото
- Музыка
- Мультфильмы
- Наука, технологии
- Новости
- Образование
- Политика
- Праздники
- Приколы
- Природа
- Происшествия
- Путешествия
- Развлечения
- Ржач
- Семья
- Сериалы
- Спорт
- Стиль жизни
- ТВ передачи
- Танцы
- Технологии
- Товары
- Ужасы
- Фильмы
- Шоу-бизнес
- Юмор
Floyd's Cycle Finding Algorithm - The Best Way to Find a Cycle in a Linked List
Floyd's cycle finding algorithm allows you to detect a cycle in a linked list, and return the entry point of the cycle, in O(n) time and O(1) space.
Code: https://gist.github.com/emcapsulation/add8a5b9b5640ab8de4b1b5a576cea45
0:00 Introduction
0:42 Hashset Method
1:30 Algorithm Walkthrough
2:41 Large Example
3:10 Explanation
5:44 Code
Algorithm Steps
1. Initialise a slow pointer (tortoise) and fast pointer (hare) pointing to the head of the linked list.
2. Do while the hare and tortoise have not met:
2a. If the hare reaches NULL, there is no cycle.
2b. Step the tortoise one node forward. Step the hare two nodes forward.
3. If the tortoise and hare meet (both pointers point to the same node), there is a cycle in the linked list. If so:
3a. Take the tortoise back to the head of the linked list.
3b. Step both the hare and tortoise pointers one node at a time.
4. They will meet at the start of the cycle, so return the pointer to that node.
Видео Floyd's Cycle Finding Algorithm - The Best Way to Find a Cycle in a Linked List канала emcapsulation
Code: https://gist.github.com/emcapsulation/add8a5b9b5640ab8de4b1b5a576cea45
0:00 Introduction
0:42 Hashset Method
1:30 Algorithm Walkthrough
2:41 Large Example
3:10 Explanation
5:44 Code
Algorithm Steps
1. Initialise a slow pointer (tortoise) and fast pointer (hare) pointing to the head of the linked list.
2. Do while the hare and tortoise have not met:
2a. If the hare reaches NULL, there is no cycle.
2b. Step the tortoise one node forward. Step the hare two nodes forward.
3. If the tortoise and hare meet (both pointers point to the same node), there is a cycle in the linked list. If so:
3a. Take the tortoise back to the head of the linked list.
3b. Step both the hare and tortoise pointers one node at a time.
4. They will meet at the start of the cycle, so return the pointer to that node.
Видео Floyd's Cycle Finding Algorithm - The Best Way to Find a Cycle in a Linked List канала emcapsulation
Комментарии отсутствуют
Информация о видео
4 октября 2025 г. 8:37:48
00:07:27
Другие видео канала




















