您的位置:首页 > 编程语言 > Go语言

Algorithms—141.Linked List Cycle

2015-07-16 17:57 781 查看
思路:经典跑圈。

/**
* 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 (true) {
if (fast==null||fast.next==null||slow==null) {
return false;
}
fast=fast.next.next;
slow=slow.next;
if (fast==slow) {
return true;
}
}
}
}

耗时:348ms,中游

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: