#-*- 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
相关文章
- LeetCode之237. Delete Node in a Linked List
- 142. Linked List Cycle II - Leetcode
- Leetcode 203 Remove Linked List Elements 链表
- [LeetCode]题解(python):086 - Partition List
- [LeetCode]题解(python):019-Remove Nth Node From End of List
- Java [Leetcode 234]Palindrome Linked List
- leetcode:142. Linked List Cycle II(Java)解答
- LeetCode 237 Delete Node in a Linked List 解题报告
- LeetCode 141. Linked List Cycle (链表循环)
- [Leetcode] Linked list cycle 判断链表是否有环