LeetCode142 — Linked List Cycle II
阿新 • • 發佈:2019-02-17
題目: 連結串列判斷是否有環,並找到入口
public class Solution142 { public ListNode detectCycle(ListNode head) { ListNode slow = head; ListNode fast = head; boolean flag = false; while(fast != null && fast.next != null){ slow = slow.next; fast = fast.next.next; if(slow == fast){ flag = true; break; } } if(!flag) return null; ListNode tmp = head; while(tmp != slow){ tmp = tmp.next; slow = slow.next; } return slow; } }