Linked List Cycle
Given a linked list, determine if it has a cycle in it.
Example
Given -21->10->4->5, tail connects to node index 1, return true
Solution
小技巧:
fast 起始位置是head.next 而不是head 否则就一开始就相等了,如果是循环链表,快指针走两步,满指针走一步,快指针早晚会追上满指针的。
while fast and fast.next:
if fast == slow:
return True
Last updated
Was this helpful?