Leetcode 141. Linked List Cycle
简单的链表题,我的代码:
class Solution(object):
def hasCycle(self, head):
"""
:type head: ListNode
:rtype: bool
"""
while head:
if head.val =='k':
return True
else:
head.val ='k'
head = head.next
return False
beat 70%+
Discuss:
1.非常有趣的回答,常常都是这个小机灵鬼
def hasCycle(self, head):
try:
slow = head
fast = head.next
while slow is not fast:
slow = slow.next
fast = fast.next.next
return True
except:
return False
本文提供了一种解决LeetCode 141题“链表环”的有趣方法,通过修改链表节点值或使用快慢指针来判断链表中是否存在环。
1670

被折叠的 条评论
为什么被折叠?



