/**
* 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 || head.next==null) return false;
ListNode current = head;
ListNode pivot = current;
if (pivot.next == null) return false;
do
{
pivot = pivot.next;
pivot = pivot.next;
current = current.next;
} while(pivot != null && pivot.next != null && pivot.next != current && pivot != current);
if ( pivot != null && (pivot.next == current || pivot == current)) return true;
return false;
}
}
No comments:
Post a Comment