建水网站开发新闻投稿平台有哪些
Problem: 141. 环形链表
文章目录
- 思路 & 解题方法
- 复杂度
- Code
思路 & 解题方法
哈希
复杂度
时间复杂度:
添加时间复杂度, 示例: O ( n ) O(n) O(n)
空间复杂度:
添加空间复杂度, 示例: O ( n ) O(n) O(n)
Code
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = Noneclass Solution:def hasCycle(self, head: Optional[ListNode]) -> bool:d = collections.defaultdict(int)cnt = 0while head:if head in d:return Trueelse:d[head] = cnthead = head.nextcnt += 1return False