题目概述(简单难度)
题目链接:
点我进入牛客网
思路与代码
思路展现
这道题目是我之前做过的一道题目,具体题解可以看我的这篇博客:
点我进入博客
代码示例
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public boolean hasCycle(ListNode head) {
if(head == null) {
return false;
}
ListNode slow = head;
ListNode fast = head;
while(fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if(fast == slow) {
return true;
}
}
return false;
}
}