leetcode 141. Linked List Cycle

C语言 码拜 8年前 (2016-05-21) 1001次浏览
Given a linked list, determine if it has a cycle in it.
本人的code:
/**
* Definition for singly-linked list.
* struct ListNode {
*     int val;
*     struct ListNode *next;
* };
*/
bool hasCycle(struct ListNode *head) {
struct ListNode *p;
if(head==NULL)
return false;
p=head;
while(p){
p=p->next;
if(p==head)
return true;
}
return false;
}
显示这个怎么解决?
leetcode 141. Linked List Cycle
解决方案

5

这题标准解法是快慢指针。

15

你的循环访问的节点顺序为0123123123…死循环,解法2楼正解

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明leetcode 141. Linked List Cycle
喜欢 (0)
[1034331897@qq.com]
分享 (0)