-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquestion_6.py
More file actions
50 lines (42 loc) · 1.07 KB
/
question_6.py
File metadata and controls
50 lines (42 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python
# @Time : 2018/5/5 下午7:44
# @Author : cancan
# @File : question_6.py
# @Function : 环形链表
"""
Question:
给定一个链表,判断链表中是否有环。
Follow up:
你能否不使用额外空间解决此题?
"""
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def hasCycle(self, head):
"""
:type head: ListNode
:rtype: bool
"""
if not head or not head.next:
return False
# n1 每次走一步
n1 = head.next
# n2 每次走两步
n2 = head.next.next
while n2:
# n2 走的比 n1 较快,如果有环则会追上 n1
if n1 == n2:
return True
n1 = n1.next
if n1:
n2 = n2.next
if n2:
n2 = n2.next
else:
return False
else:
return False
return False