141. Linked List Cycle
2017-09-05 21:20
429 查看
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
判断链表是否有环的技巧:一个走两步,一个走一步,查看是否汇合
class Solution(object):
def hasCycle(self, head):
p = head
q = head
has_cycle = False
try:
while(p and p.next):
p = p.next
q = q.next.next
if p == q:
has_cycle = True
break
except AttributeError:
pass
return has_cycle
Follow up:
Can you solve it without using extra space?
判断链表是否有环的技巧:一个走两步,一个走一步,查看是否汇合
class Solution(object):
def hasCycle(self, head):
p = head
q = head
has_cycle = False
try:
while(p and p.next):
p = p.next
q = q.next.next
if p == q:
has_cycle = True
break
except AttributeError:
pass
return has_cycle
相关文章推荐
- 【leetcode】【141】Linked List Cycle
- 【Leetcode-Easy-141】Linked List Cycle
- LeetCode 141 -Linked List Cycle ( JAVA )
- Easy 141题 Linked List Cycle Medium 142题 Linked List Cycle II
- 141. Linked List Cycle LeetCode
- LeetCode 141 Linked List Cycle
- [leetcode 141] Linked List Cycle----判断链表是否为循环链表
- 【leetcode】【单链表】【141】Linked List Cycle
- leetcode 141: Linked List Cycle
- [LeetCode]题解(python):141-Linked List Cycle
- Leetcode#141 Linked List Cycle
- LeetCode,141:Linked List Cycle
- [leedcode 141] Linked List Cycle
- 141 linked list cycle
- 141. Linked List Cycle leetcode list
- leetcode note--leetcode 141 Linked List Cycle
- LeetCode 141 — Linked List Cycle(C++ Java Python)
- 019-141-Linked List Cycle 判断链表是否有环
- LeetCode.142(141) Linked List Cycle && Linked List Cycle (II)
- Leetcode#141(链表)-Linked List Cycle