# 用python 判断一个单链表是否有环

## 用python 判断一个单链表是否有环.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@Time    : 2019/1/12 00:59
@File    : has_circle.py
@Author  : frank.chang@shoufuyou.com

Easy

1231

93

Favorite

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

To represent a cycle in the given linked list,
we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to.
If pos is -1, then there is no cycle in the linked list.

Example 1:

Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: There is a cycle in the linked list, where tail connects to the second node.

Example 2:

Input: head = [1,2], pos = 0
Output: true
Explanation: There is a cycle in the linked list, where tail connects to the first node.

Example 3:

Input: head = [1], pos = -1
Output: false
Explanation: There is no cycle in the linked list.

Can you solve it using O(1) (i.e. constant) memory?

Accepted
340,579
Submissions
971,443

"""

def __init__(self, value):
self.value = value
self.next = None

class Solution1:
"""
思路分析:
判断一个单链表是否有环,
可以用 set 存放每一个 节点, 这样每次 访问后把节点丢到这个集合里面.
其实 可以遍历这个单链表, 访问过后,
如果这个节点 不在 set  里面, 把这个节点放入到 set 集合里面.
如果这个节点在  set 里面 , 说明曾经访问过, 所以这个链表有重新 走到了这个节点, 因此一定有环.

如果链表都走完了, 把所有的节点都放完了. 还是没有重复的节点, 那说明没有环.

"""

mapping = set()

flag = False

while p:

if p not in mapping:
else:
flag = True
break
p = p.next

return flag


fast 有可能在 慢指针的后面, 或者前面, 无论如何 快指针 是必慢指针走的快的 , 所以 只要有环 一定可以 和慢指针来一次相遇.

class Solution:
"""
定义 两个指针, 一个快指针fast, 一个慢指针slow,  快指针一次都两步,慢指针一次走一步.
如果 两个指针相遇了, 则说明链表是有环的.
如果 fast 都走到null了, 还没有相遇则说明没有环.
"""

flag = False
return flag

while fast is not slow:

if fast.next is None or fast.next.next is None:
# no circle
return flag
fast = fast.next.next
slow = slow.next

# 相遇肯定有环
if fast is slow:
# hasCircle
flag = True

return flag


## 第二次做DAY20201130

### 141. 环形链表

输入：head = [3,2,0,-4], pos = 1



输入：head = [1,2], pos = 0



输入：head = [1], pos = -1



• 链表中节点的数目范围是 [0, 104]
• -105 <= Node.val <= 105
• pos-1 或者链表中的一个 有效索引

#### 方法1 路径记录法

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
def hasCycle(self, head: ListNode) -> bool:
visited = set()
if cur not in visited:
else:
return True
return False

class Solution02:
def hasCycle(self, head: ListNode) -> bool:
seen = set()
return True
return False



#### 方法2 快慢指针法

##### 第1种解法
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
def hasCycle(self, head: ListNode) -> bool:
return False

while fast and fast.next:
if fast ==slow:
return True
fast= fast.next.next
slow = slow.next

return False

##### 另一种写法
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
def hasCycle(self, head: ListNode) -> bool:
return False

while slow != fast:
if fast is None or fast.next is None:
return False
fast = fast.next.next
slow = slow.next

return True


01-13 1万+

08-05 6814
11-28 1228
06-25 9717
04-01 1586
02-01 386
09-01 3090
10-24 89
03-21 549
05-03 90