LEETCODE,[Lintcode]102. Linked List Cycle/[Leetcode]

 2023-10-18 阅读 28 评论 0

摘要:106. Convert Sorted List to Binary Search Tree/109. Convert Sorted List to Binary Search Tree 本題難度: Medium/EasyTopic: Linked ListDescription A linked list is given such that each node contains an additional random pointer which could point to any node

106. Convert Sorted List to Binary Search Tree/109. Convert Sorted List to Binary Search Tree

  • 本題難度: Medium/Easy
  • Topic: Linked List

Description

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.

Return a deep copy of the list.

Challenge
Could you solve it with O(1) space?
```

我的代碼

"""
Definition of ListNode
class ListNode(object):def __init__(self, val, next=None):self.val = valself.next = next
"""class Solution:"""@param head: The first node of linked list.@return: True if it has a cycle, or false"""def hasCycle(self, head):# write your code heretry:slow = headfast = head.nextwhile(slow is not fast):slow = slow.nextfast = fast.next.nextreturn Trueexcept:return False

思路

快慢指針
用try..except

LEETCODE、轉載于:https://www.cnblogs.com/siriusli/p/10375610.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/149493.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息