2019-03-07 15:06:13 429浏览
今天扣丁学堂Python培训老师给大家介绍一下关于Python3实现的判断环形链表算法,涉及Python针对环形链表的遍历、判断相关操作技巧详解,希望对学习Pyythn开发的同学有所帮助,下面我们一起来看一下吧。
# 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
"""
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if fast == slow:
return True
return False
# 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:
return False
cur = head.next
while cur:
if cur.next == head:
return True
cur = cur.next
return False
最后想要了解更多关于Python方面内容的小伙伴,请关注扣丁学堂Python培训官网、微信等平台,扣丁学堂IT职业在线学习教育平台为您提供Python学习视频教程共大家学习,Python培训后的前景无限,行业薪资和未来的发展会越来越好的,扣丁学堂老师精心推出的Python视频直播课定能让你快速掌握Python从入门到精通开发实战技能。扣丁学堂Python技术交流群:279521237。
【关注微信公众号获取更多学习资料】