zoukankan      html  css  js  c++  java
  • Leetcode练习(Python):链表类:第141题:环形链表:给定一个链表,判断链表中是否有环。 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。

    题目:
    环形链表:给定一个链表,判断链表中是否有环。  为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。  
    思路:
    双指针
    程序:
    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    class Solution:
        def hasCycle(self, head: ListNode) -> bool:
            if not head:
                return None
            if not head.next:
                return None
            index1 = head
            index2 = head
            while index2 and index2.next:
                index2 = index2.next.next
                index1 = index1.next
                if index1 == index2:
                    return True
            return False
  • 相关阅读:
    React获取文本框的值
    Ant-design正则判断_未输入用户名和密码点击按钮提示输入
    Redux的简单使用
    React后台管理系统 路由守卫
    移动端如何定义字体font-family
    meta基础知识
    闭包
    前端优化
    jacaScript数组
    分享一款强大的图片查看器插件,手机PC 通吃,功能超级齐全!
  • 原文地址:https://www.cnblogs.com/zhuozige/p/12813399.html
Copyright © 2011-2022 走看看