-
标签:
链表
双指针
-
难度:
简单
- 题目描述


- 我的解法
如果快慢指针相遇,则链表中有环。
一个通俗的解释: 为什么用快慢指针找链表的环,快指针和慢指针一定会相遇? - 飘逸的h的回答 - 知乎
# 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 == None:
return False
slow, fast = head, head
while (fast and fast.next):
slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return False
- 其他解法
暂略。
网友评论