/
141_LinkedListCycle.js
90 lines (74 loc) · 2.45 KB
/
141_LinkedListCycle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const assert = require('assert');
/**
* Definition for singly-linked list.
*
*/
function ListNode(val)
{
this.val = val;
this.next = null;
}
/**
* Given head, the head of a linked list, determine if the linked list has a cycle in it.
*
* There is a cycle in a linked list if there is some node in the list that can be
* reached again by continuously following the next pointer.
*
* Return true if there is a cycle in the linked list. Otherwise, return false.
*
* @param {ListNode} head
* @return {boolean}
*/
var hasCycle = function(head)
{
let current_node = head;
// ------------------------------------------------------------------------
// Check Inputs:
// ------------------------------------------------------------------------
if (head == undefined)
{
return false;
}
// ------------------------------------------------------------------------
// Store references to the nodes we've seen.
// ------------------------------------------------------------------------
let seen_nodes = new Set();
// ------------------------------------------------------------------------
// Scan through the list and if we find a node we've seen
// before, return false as we've got a cycle:
// ------------------------------------------------------------------------
while (current_node.next != undefined && current_node.next != null)
{
if (seen_nodes.has(current_node.next) == true)
{
return true;
}
seen_nodes.add(current_node.next);
current_node = current_node.next;
}
// ------------------------------------------------------------------------
// We never saw a node we've already visited again, so no
// cycles exist:
// ------------------------------------------------------------------------
return false;
};
describe('141_LinkedListCycle.js', function ()
{
it('should return true', function ()
{
var node1 = new ListNode(1);
var node2 = new ListNode(9);
node1.next = node2;
node2.next = node1;
let results = hasCycle(node1);
assert.deepEqual(true, results);
});
it('should return false', function ()
{
var node1 = new ListNode(1);
var node2 = new ListNode(9);
node1.next = node2;
let results = hasCycle(node1);
assert.deepEqual(false, results);
});
});