由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - How can one determine whether a singly linked list has a cycle?
相关主题
sorted linked list里insert一个node想成为嵌入式程序员应知道的0x10个基本问题 zz
C++ 面试题目分享(1)刚才看到小尾羊的一个面试题
copy link with random additional pointers一道链表题及其变种
remove a node (and its memory) from a doubly linked listF家电面
那个双堆求median的能不能这样做?各位刷友,leetcode里的题目:Copy List with Random Pointer
c/c++ double pointer研究[电话面试] Amazon First Round
"简单的"linklist的问题C++ Q62: loop in a linked list (Bloomberg)
有人同看Populating Next Right Pointers in Each Node II的recursive写法么?两个面试题目讨论一下
相关话题的讨论汇总
话题: 8233话题: pointer话题: node话题: linked话题: singly
进入JobHunting版参与讨论
1 (共1页)
b****n
发帖数: 464
1
Keep 
track 
of
 two 
pointers 
in the linked&
#8233; list,
 and 
start 
them
 at 
the

beginning 
of 
the 
linked 
list.


At 
each 
iteration 
of 
the 

algorithm,
advance 
the 
first
 pointer 
by
one 
node 
and 
the
 second
 pointer by

two 
nodes.


If 
the 
two 
pointers 
are

ever 
the 
same 
(other 
than 
at
 the
beginning 
of
 the
 algorithm), 
then 
there &
#8233;is
 a 
cycle.


If
 a
 pointer
 ever 
reaches

the 
end
 of
 the
 inked
 list
 before
the
 pointers 
are 
the 
same,
 then there &#
8233;is 
no 
cycle.

我看到的答案是这样的。但是我觉得不太对啊,比如 node1 指向 node2 指向 node3
指向 node4 指向 node5 指向 node6 指向 node2
g**u
发帖数: 504
2
they will meet at node 6.

linked&
8233;
8233;
&

【在 b****n 的大作中提到】
: Keep 
track 
of
 two 
pointers 
in the linked&
: #8233; list,
 and 
start 
them
 at 
the

: beginning 
of 
the 
linked 
list.
: 

At 
each 
iteration 
of 
the 

: algorithm,
advance 
the 
first
 pointer 
by
: one 
node 
and 
the
 second
 pointer by

: two 
nodes.
: 

If 
the 
two 
pointers 
are

: ever 
the 
same 
(other 
than 
at
 the
: beginning 
of
 the
 algorithm), 
then 
there &

b****n
发帖数: 464
3
为什么呢?
第1步: pointer 1 at node 1; pointer 2 at node 1;
第2步: pointer 1 at node 2; pointer 2 at node 3;
第3步: pointer 1 at node 3; pointer 2 at node 5;
第4步: pointer 1 at node 4; pointer 2 at node 2; (结束)
还是说,至此不算结束,还继续?
第5步: pointer 1 at node 5; pointer 2 at node 4;
第6步: pointer 1 at node 6; pointer 2 at node 6;
答案其实还有一句,就是说pointer 2不一定要只跳一格,可以每次跳n (n>1)格,
那么,是不是说,根据圈的大小,有时候要绕比较多圈,两个pointer才刚好遇得上?
而且,反正有圈,两个pointer如果遇不上,就会一直转下去?
z*********8
发帖数: 2070
4
这当然不算结束
n->next == NULL 才算结束, 有环的情况永远不会结束,直到相遇

【在 b****n 的大作中提到】
: 为什么呢?
: 第1步: pointer 1 at node 1; pointer 2 at node 1;
: 第2步: pointer 1 at node 2; pointer 2 at node 3;
: 第3步: pointer 1 at node 3; pointer 2 at node 5;
: 第4步: pointer 1 at node 4; pointer 2 at node 2; (结束)
: 还是说,至此不算结束,还继续?
: 第5步: pointer 1 at node 5; pointer 2 at node 4;
: 第6步: pointer 1 at node 6; pointer 2 at node 6;
: 答案其实还有一句,就是说pointer 2不一定要只跳一格,可以每次跳n (n>1)格,
: 那么,是不是说,根据圈的大小,有时候要绕比较多圈,两个pointer才刚好遇得上?

1 (共1页)
进入JobHunting版参与讨论
相关主题
两个面试题目讨论一下那个双堆求median的能不能这样做?
请教一个C++的小问题: Node *&curr Vs Node *currc/c++ double pointer研究
alternative solution to detect cycle in linked list"简单的"linklist的问题
谁对design pattern比较熟?有人同看Populating Next Right Pointers in Each Node II的recursive写法么?
sorted linked list里insert一个node想成为嵌入式程序员应知道的0x10个基本问题 zz
C++ 面试题目分享(1)刚才看到小尾羊的一个面试题
copy link with random additional pointers一道链表题及其变种
remove a node (and its memory) from a doubly linked listF家电面
相关话题的讨论汇总
话题: 8233话题: pointer话题: node话题: linked话题: singly