/* Detect a cycle in a Linked List. Note that the head may be 'null' if the list is empty. Node is defined as var Node = function(data) { this.data = data; this.next = null; } */ // This is a "method-only" submission. // You only need to complete this method. function hasCycle(head) { // console.log("starting..."); var nodesToReset = []; var ret = false; for (var node = head; node != null; node = node.next) { // console.log("node.data = " + node.data + ", node.next.data = " + node.next.data + ", node.seen = " + node.seen); if (node.seen === true) { ret = true; break; } node.seen = true; nodesToReset.push(node); } nodesToReset.forEach(function(node) { // console.log("resetting node.data = " + node.data); node.seen = false; }); return ret; }