链表之环形链表


上题目:

leetcode 141 简单

 

 

总体思路:

双指针,即通过使用一快一慢不同速度的两个指针来遍历链表,如果有环,快的总会与慢的相遇。

上代码:

 1 class Solution:
 2     def has_cycle(self, head):
 3         """
 4         :type head:ListNode
 5         :rtype:bool
 6         """
 7         if head == None:
 8             return False
 9         
10         fast, slow = head, head
11         # 这里快指针fast一次走两步,慢指针slow一次走一步
12         while fast.next != None and fast.next.next != None:
13             slow = slow.next
14             fast = fast.next.next
15             if slow == fast:
16                 return True
17                 
18         return False

 

题目Ⅱ:

leetcode 142 中等

 

 

总体思路:

待解决

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM