天天看點

LeetCode: Linked List Cycle

今天終于鼓起勇氣來做leetcode了,按照難度增加開始的,做了最簡單的10道題目,有的還是挺有意思的。

總結寫一下

Given a linked list, determine if it has a cycle in it.

Follow up:

Can you solve it without using extra space?

代碼如下:

LeetCode: Linked List Cycle

這道題目的巧妙之處在于用了兩個步長來traverse整個linked list,當有重合發生的時候則說明有cycle存在。代碼不是很複雜。