8.18 第一次做,无法 AC
8.19 可以 AC


第一次复习
9.15 可以 A,但不太顺畅

题目描述


力扣:https://leetcode-cn.com/problems/linked-list-cycle/

解题思路:快慢双指针


官方题解:https://leetcode-cn.com/problems/linked-list-cycle/solution/huan-xing-lian-biao-by-leetcode-solution/

官方的题解文字表述很不错,可以看,但是代码太冗余了!

代码用我下面的这个就行!

  1. public class Solution {
  2. public boolean hasCycle(ListNode head) {
  3. ListNode slow = head, fast = head;
  4. while(fast != null && fast.next != null) {
  5. slow = slow.next;
  6. fast = fast.next.next;
  7. if(slow == fast) return true;
  8. }
  9. return false;
  10. }
  11. }