您的位置:首页 > 其它

LeetCode 141. Linked List Cycle

2015-06-24 20:52 363 查看
/**
* 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;
while (head != null && head.next != null) {
head = head.next.next;
slow = slow.next;
if (head == slow) return true;
}
return false;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: