判断一个list是不是循环队列!本题的一种的解法是使用快慢指针进行操做。 java
快、慢指针起点相同,但快指针比慢指针每次都快一步。 指针
/** * 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) { ListNode fast = head; ListNode slow = head; while(fast!=null && fast.next!=null){ fast = fast.next.next; slow = slow.next; if(fast == slow){ return true; } } return false; } }