mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2026-03-13 15:21:15 +08:00
implemented CycleDetectionII code in LinkedList (#1482)
* implemented CycleTectionII code * changes made per review by appgurueu * made the changes per review by appgurueu * changes made per review by appgurueu * did some changes * fixed the test file with prettier * Simplify code, renames for clarity --------- Co-authored-by: Lars Mueller <appgurulars@gmx.de>
This commit is contained in:
39
Data-Structures/Linked-List/test/CycleDetectionII.test.js
Normal file
39
Data-Structures/Linked-List/test/CycleDetectionII.test.js
Normal file
@@ -0,0 +1,39 @@
|
||||
import { findCycleStart } from '../CycleDetectionII'
|
||||
import { Node } from '../SinglyLinkedList'
|
||||
|
||||
describe('Detect Cycle', () => {
|
||||
it('no cycle', () => {
|
||||
const head = new Node(1)
|
||||
head.next = new Node(2)
|
||||
|
||||
expect(findCycleStart(head)).toBeNull()
|
||||
})
|
||||
|
||||
it('simple cycle', () => {
|
||||
const head = new Node(1)
|
||||
head.next = new Node(2)
|
||||
head.next.next = new Node(3)
|
||||
head.next.next.next = head.next // Creates a cycle
|
||||
|
||||
expect(findCycleStart(head)).toBe(head.next)
|
||||
})
|
||||
|
||||
it('long list with cycle', () => {
|
||||
const head = new Node(1)
|
||||
head.next = new Node(2)
|
||||
head.next.next = new Node(3)
|
||||
head.next.next.next = new Node(4)
|
||||
head.next.next.next.next = new Node(5)
|
||||
head.next.next.next.next.next = head.next.next // Cycle
|
||||
|
||||
expect(findCycleStart(head)).toBe(head.next.next)
|
||||
})
|
||||
|
||||
it('cycle on last node', () => {
|
||||
const head = new Node(1)
|
||||
head.next = new Node(2)
|
||||
head.next.next = head
|
||||
|
||||
expect(findCycleStart(head)).toBe(head)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user