mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-09-20 07:43:04 +08:00
hare and tortoise algoritm of cycle detection in a linked list
This commit is contained in:
parent
8c5e5f4f0d
commit
439308c715
@ -0,0 +1,3 @@
|
||||
# Floyd's Cycle Detection Algorithm (aka Hare and Tortoise Algorithm)
|
||||
Floyd algorithm for finding the cycles in a linked list is used to determine presence of a cycle in the linked list using constant space and linear time (i.e. space complexity = O(1) and time comoplexity = O(n), where n is number of nodes in the list).
|
||||
The algorithm uses two pointers where one pointer moves a littile faster (the hare pointer). If at any point of traversal, the fast pointer meets the slow pointer, we can say that we have a cycle in the linked list. Conversly if the fast pointer hits a null, we have reached the end of list and no cycle is present.
|
@ -0,0 +1,36 @@
|
||||
import LinkedList from "../../../../data-structures/linked-list/LinkedList";
|
||||
import LinkedListNode from "../../../../data-structures/linked-list/LinkedListNode";
|
||||
import hareAndTortoise from "../hareTortoise";
|
||||
|
||||
function getTail(linkedList){
|
||||
let tail = linkedList.head;
|
||||
while(tail.next){
|
||||
tail=tail.next;
|
||||
}
|
||||
return tail;
|
||||
}
|
||||
describe('cycleDetection',()=>{
|
||||
it('should be able to properly detect no cycles in a linked list',()=>{
|
||||
let simpleLinkedList = new LinkedList();
|
||||
simpleLinkedList
|
||||
.append(1)
|
||||
.append(2)
|
||||
.append(3);
|
||||
|
||||
const containsCycle = hareAndTortoise(simpleLinkedList);
|
||||
expect(containsCycle).toEqual(false);
|
||||
});
|
||||
it('should detect a cycle in the linked list',()=>{
|
||||
let cyclicLinkedList = new LinkedList();
|
||||
cyclicLinkedList
|
||||
.append(1)
|
||||
.append(2)
|
||||
.append(3)
|
||||
.append(4);
|
||||
const tail = getTail(cyclicLinkedList);
|
||||
tail.next = cyclicLinkedList.head;
|
||||
const containsCycle = hareAndTortoise(cyclicLinkedList);
|
||||
expect(containsCycle).toEqual(true);
|
||||
});
|
||||
|
||||
})
|
@ -0,0 +1,21 @@
|
||||
/**
|
||||
* @param {LinkedList} linkedList
|
||||
* @returns {boolean}
|
||||
*/
|
||||
|
||||
export default function hareAndTortoise(linkedList){
|
||||
let hare = linkedList.head;
|
||||
let tortoise = linkedList.head;
|
||||
while(hare && hare.next && hare.next.next){
|
||||
hare = hare.next.next;
|
||||
tortoise = tortoise.next;
|
||||
//iterate till "hare" has not reached the end of list
|
||||
if(hare == tortoise){
|
||||
// we got a cycle
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
// no cycle just the end of list
|
||||
return false;
|
||||
}
|
Loading…
Reference in New Issue
Block a user