package code; /* * 141. Linked List Cycle * 颿ï¼é¾è¡¨æ¯å¦æç¯ * é¾åº¦ï¼Easy * åç±»ï¼Linked List, Two Pointers * æè·¯ï¼å¿«æ ¢æé * lc142 */ public class lc141 { public class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } public boolean hasCycle(ListNode head) { if(head==null||head.next==null) return false; ListNode slow = head; ListNode fast = head.next; while(fast!=null&&fast.next!=null){ //注æå¤ææ¡ä»¶ï¼slowä¸å®ä¸çäºnullï¼ä¸ç¨å¤æäº slow = slow.next; fast = fast.next.next; if(fast==slow) return true; } return false; } }