zoukankan      html  css  js  c++  java
  • 环形链表2

     双指针:

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

    class Solution:
        def detectCycle(self, head: ListNode) -> ListNode:
            if head is None or head.next is None:
                return 
            slow = fast = head
            while slow and fast and fast.next :
                slow = slow.next
                fast = fast.next.next
                if slow is fast:
                    break
            if fast is None or fast.next is None: #表明没有环
                return 
            tmp = head
            while tmp is not slow:
                tmp = tmp.next
                slow = slow.next
            return slow
     
    语言特性:
    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None

    class Solution:
        def detectCycle(self, head: ListNode) -> ListNode:
    #        if head is None and head.next is None:
     #           return head 
            hashmap = []
            while head:
                if head in hashmap:
                    return head
                hashmap.append(head)
                head = head.next
            return None
  • 相关阅读:
    使用鼠标
    TCP编程函数和步骤
    ASP.NET MVC+EF框架+EasyUI实现
    线性表简介
    一个项目的简单开发流程——需求、数据库、编码
    图像处理网络资源
    OPENCV 中的图像旋转与缩放
    命令行下面使用MAKEFILE方式编译OPENCV程序
    OpenCV In Thanksgiving Day
    OpenCV 下面的图像亮度变换 Intensity transformation
  • 原文地址:https://www.cnblogs.com/shamoguzhou/p/15306596.html
Copyright © 2011-2022 走看看