
解法1:双指针
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
/*双指针:一快一满,
slow每次只走一步;fast每次走两步;
如果是有循环的环,快的总会追上慢的,否则则没有*/
public class Solution {
public boolean hasCycle(ListNode head) {
if (head == null||head.next==null) return false;
ListNode slow = head;
ListNode fast = head.next;
while(slow!=fast ){
if(fast==null||fast.next==null){
return false;
}
slow = slow.next;
fast = fast.next.next;
}
return true;
}
}
解法2:Hash table

相关文章:
-
2021-06-17
-
2022-12-23
-
2021-10-27
-
2021-09-28
-
2021-12-25
-
2021-12-15
-
2021-12-22
-
2021-07-02