js-practice/test/list/linked-list-cycle.test.js

25 lines
539 B
JavaScript
Raw Normal View History

2020-05-09 07:12:18 +08:00
import { hasCycle } from '../../src/list/linked-list-cycle'
function ListNode (val) {
this.val = val
this.next = null
}
const arrToList = (arr) => {
const head = new ListNode(arr[0])
let current = head
for (let n = 1, len = arr.length; n < len; n++) {
current.next = new ListNode(arr[n])
current = current.next
}
return head
}
test('环形链表', () => {
const list = arrToList([1, 2, 3])
list.next.next.next = list
expect(hasCycle(list)).toBe(true)
expect(hasCycle(arrToList([1, 2, 3]))).toBe(false)
})