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
  • 相关阅读:
    灰度图转换
    OGRE分析之文件系统 (1)
    屏幕截图
    [GP]template必须定义于头文件中
    OGRE分析之设计模式
    ON_COMMAND_RANGE和ON_UPDATE_COMMAND_UI_RANGE
    使用SkinMagic Toolkit美化界面(II)
    Single Sign On for Windows and Linux
    "C compiler cannot create executables"
    How to Create a First C Program on Linux
  • 原文地址:https://www.cnblogs.com/shamoguzhou/p/15306596.html
Copyright © 2011-2022 走看看