9月 6 2017 Online Judge►LeetCode [LeetCode] 141 - Linked List Cycle 題意在沒有額外空間的情況下判斷 Linked List 是否有環。 解法使用兩個指針遍歷這個 Linked List 。一個一次走兩步,另一個則一次走一步,若有環則必定會合同一個點。 程式1234567891011121314151617181920212223242526/** * Definition for singly-linked list. * function ListNode(val) { * this.val = val; * this.next = null; * } *//** * @param {ListNode} head * @return {boolean} */var hasCycle = function(head) { if ( head === null ) return false; let fast = head; let slow = head; while ( fast.next && fast.next.next ){ fast = fast.next.next; slow = slow.next; if ( fast === slow ){ return true; } } return false;}; Newer [LeetCode] 102 - Binary Tree Level Order Traversal Older [LeetCode] 142 - Linked List Cycle II