How can one determine whether a singly linked list has a cycle?# JobHunting - 待字闺中
b*n
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
#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