diff --git a/linked-list-cycle/limlimjo.js b/linked-list-cycle/limlimjo.js new file mode 100644 index 000000000..2ea96537b --- /dev/null +++ b/linked-list-cycle/limlimjo.js @@ -0,0 +1,29 @@ +// 시간 복잡도: O(n) +// 공간 복잡도: O(1) +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ + +/** + * @param {ListNode} head + * @return {boolean} + */ +var hasCycle = function (head) { + let slow = head; + let fast = head; + + while (fast !== null && fast.next !== null) { + slow = slow.next; + fast = fast.next.next; + + if (slow === fast) { + return true; + } + } + + return false; +};