#-*- coding: UTF-8 -*-
#Method:快慢指针法,建立虚表头,快指针走两步,慢指针走一步,若存在环,则快指针会追上慢指针
# 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 head.next==None or head==None or head.next.next==None:
return False
dummy=listNode(0)
dummy.next=head
fast=dummy
slow=dummy
while pre!=None and cur!=None and cur.next!=None:
slow=slow.next
fast=fast.next.next
if pre==cur:
return True
return False
相关文章
- 12-19【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)
- 12-19[LeetCode203]Remove Linked List Elements
- 12-19LeetCode解题报告—— Reverse Linked List II & Restore IP Addresses & Unique Binary Search Trees II
- 12-19[LeetCode] 141. Linked List Cycle 单链表判圆算法
- 12-19【Leetcode】141. 环形链表(Linked List Cycle)
- 12-19LeetCode 141. Linked List Cycle--百度面试编程题--C++,Python解法
- 12-19leetcode 206. Reverse Linked List
- 12-19[LeetCode] 426. Convert Binary Search Tree to Sorted Doubly Linked List
- 12-19LeetCode--LinkedList--203. Remove Linked List Elements(Easy)
- 12-19leetcode:Remove Linked List Elements