mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +08:00
Refactor Cycledetection.js and added it's test. (#1099)
This commit is contained in:
@ -1,32 +1,24 @@
|
|||||||
/**
|
/**
|
||||||
* A LinkedList based solution for Detect a Cycle in a list
|
* A LinkedList based solution for Detecting a Cycle in a list.
|
||||||
* https://en.wikipedia.org/wiki/Cycle_detection
|
* https://en.wikipedia.org/wiki/Cycle_detection
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function main () {
|
function detectCycle (head) {
|
||||||
/*
|
/*
|
||||||
Problem Statement:
|
Problem Statement:
|
||||||
Given head, the head of a linked list, determine if the linked list has a cycle in it.
|
Given head, the head of a linked list, determine if the linked list has a cycle in it.
|
||||||
|
|
||||||
Note:
|
|
||||||
* While Solving the problem in given link below, don't use main() function.
|
|
||||||
* Just use only the code inside main() function.
|
|
||||||
* The purpose of using main() function here is to avoid global variables.
|
|
||||||
|
|
||||||
Link for the Problem: https://leetcode.com/problems/linked-list-cycle/
|
Link for the Problem: https://leetcode.com/problems/linked-list-cycle/
|
||||||
*/
|
*/
|
||||||
const head = '' // Reference to head is given in the problem. So please ignore this line
|
if (!head) { return false }
|
||||||
let fast = head
|
|
||||||
let slow = head
|
|
||||||
|
|
||||||
while (fast != null && fast.next != null && slow != null) {
|
let slow = head
|
||||||
|
let fast = head.next
|
||||||
|
while (fast && fast.next) {
|
||||||
|
if (fast === slow) { return true }
|
||||||
fast = fast.next.next
|
fast = fast.next.next
|
||||||
slow = slow.next
|
slow = slow.next
|
||||||
if (fast === slow) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
main()
|
export { detectCycle }
|
||||||
|
31
Data-Structures/Linked-List/test/CycleDetection.test.js
Normal file
31
Data-Structures/Linked-List/test/CycleDetection.test.js
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
import { detectCycle } from '../CycleDetection'
|
||||||
|
import { Node } from '../SinglyLinkedList'
|
||||||
|
|
||||||
|
describe('Detect Cycle', () => {
|
||||||
|
it('should detect loop and return true', () => {
|
||||||
|
// Creating list and making a loop
|
||||||
|
const headNode = new Node(10)
|
||||||
|
headNode.next = new Node(20)
|
||||||
|
headNode.next.next = new Node(30)
|
||||||
|
headNode.next.next.next = new Node(40)
|
||||||
|
headNode.next.next.next.next = headNode
|
||||||
|
expect(detectCycle(headNode)).toEqual(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should not detect a loop and return false', () => {
|
||||||
|
// Case 0: When head is null, there is no loop.
|
||||||
|
expect(detectCycle(null)).toEqual(false)
|
||||||
|
const headNode = new Node(10)
|
||||||
|
|
||||||
|
// Case 1: List with single node doesn't have any loop
|
||||||
|
expect(detectCycle(headNode)).toEqual(false)
|
||||||
|
|
||||||
|
headNode.next = new Node(20)
|
||||||
|
headNode.next.next = new Node(30)
|
||||||
|
headNode.next.next.next = new Node(40)
|
||||||
|
headNode.next.next.next.next = new Node(50)
|
||||||
|
|
||||||
|
// Case 2: List not having any loops
|
||||||
|
expect(detectCycle(headNode)).toEqual(false)
|
||||||
|
})
|
||||||
|
})
|
Reference in New Issue
Block a user