leetcode 【 Linked List Cycle II 】 python 实现
公司和学校事情比较多,隔了好几天没刷题,今天继续刷起来。
题目:
Given a linked list, return the node where the cycle begins. If there is no cycle, return null
.
Follow up:
Can you solve it without using extra space?
代码:oj 测试通过 Runtime: 596 ms
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
# @param head, a ListNode
# @return a list node
def detectCycle(self, head):
if head is None or head.next is None:
return None dummyhead = ListNode(0)
dummyhead.next = head p1 = ListNode(0)
p1.next = head
p2 = ListNode(0)
p2.next = head first_meet = None
while p1 is not None and p2 is not None:
if p1 == p2:
first_meet = p1
break
else:
p1 = p1.next
p2 = p2.next
if p2 is not None:
p2 = p2.next result = None
if first_meet is None:
return None
else:
p2 = dummyhead
while p1 is not None and p2 is not None:
if p1.next == p2.next:
result = p1.next
break
else:
p1 = p1.next
p2 = p2.next
return result
思路:
主要利用快慢指针的思想。
自己没太多思路,直接参考下面几篇靠谱的日志:
http://www.cnblogs.com/hiddenfox/p/3408931.html
http://blog.csdn.net/cs_guoxiaozhu/article/details/14209743
如果链表中没有循环自不必说;
如果有循环:
快慢指针第一次相遇之后,把一个指针重新指向head,然后两个指针相同速度往前走;
两个指针第二次相遇的位置就是循环开始的位置
Tips:
自己在实现的时候犯了一个错误,迭代p1 p2 找到二次相遇的位置 直接返回p1,这里迷糊了:应该迭代p1 p2判断p1.next与p2.next相等,再返回p1.next;这样返回的点才是原来链表中的,而不是构造出来的p1。
leetcode 【 Linked List Cycle II 】 python 实现的更多相关文章
- leetcode Linked List Cycle II python
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...
- LeetCode Linked List Cycle II 和I 通用算法和优化算法
Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...
- LeetCode: Linked List Cycle II 解题报告
Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...
- [LeetCode] Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [Leetcode] Linked list cycle ii 判断链表是否有环
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follo ...
- [LeetCode] Linked List Cycle II, Solution
Question : Given a linked list, return the node where the cycle begins. If there is no cycle, return ...
- [LeetCode]Linked List Cycle II解法学习
问题描述如下: Given a linked list, return the node where the cycle begins. If there is no cycle, return nu ...
- LeetCode——Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- Leetcode Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [LeetCode] Linked List Cycle II 链表环起始位置
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
随机推荐
- Android 应用开机自启和无需权限开启悬浮框
开机自启主要自定义广播接收类,且需要在清单文件中注册,不要在代码中动态注册. <uses-permission android:name="android.permission.REC ...
- Android 桌面悬浮窗效果实现,仿360手机卫士悬浮窗效果
首先是一个小的悬浮窗显示的是当前使用了百分之多少的内存,点击一下小悬浮窗,就会弹出一个大的悬浮窗,可以一键加速.好,我们现在就来模拟实现一下类似的效果. 先谈一下基本的实现原理,这种桌面悬浮窗的效果很 ...
- Eucalyptus-NC管理
1.前言 Elastic Utility Computing Architecture for Linking Your Programs To Useful Systems (Eucalyptus) ...
- ADO数据库编程详解(C++)----初级入门篇
一.概述 ADO即Microsoft ActiveXData Object,是Microsoft继ODBC之后,基于OLE DB技术的一种数据库操作技术,使您能够编写通过 OLE DB提供者对在数据库 ...
- 解决Android Studio和Android SDK Manager无法在线更新的问题[转]
升级时提示 Connection failed. Please check your network connection and try again 修改安装目录下bin\studio.exe.vm ...
- webpack打包过程如何调试?
本文适用于已经会使用webpack的前端开发人员,但是想进一步了解webpack细节和进阶. 首先请读者按照我前一篇文章 Webpack 10分钟入门介绍的步骤,在本地搭建一个webpack的hell ...
- 打造颠覆你想象中的高性能,轻量级的webform框架---js直接调后台的封装(第三天)
如果你没有看我第二天写的内容的,我想你是看不懂的!!!! 好了,废话不多说,怎么才能让我们的代码变得牛逼起来呢?怎么封装我们的代码呢?我们不可能 每个页面都需要那样写吧,那我们来一步一步来封装 我们的 ...
- iOS 不同的崩溃类型
http://m.blog.csdn.net/kangguang/article/details/62501490 用汇编语言编写的软件跟用脚本或标记语言编写的Web应用的差别在于,前者在出现问题时会 ...
- 软件杯python-flask遇到的坑有感!
大三下,对于我考研的人来说,时间不要太紧张,参加软件杯也是系主任要求,题目是公共地点人流量的检测,个人还是个菜鸟,但是把遇到的一些大家可能不小心会出现的问题贴出来,困扰我很久,还没睡好觉!!! Que ...
- Mint UI文档
Mint UI文档:http://elemefe.github.io/mint-ui/#/ 一.Mint UI的安装和基本用法. 1.NPM :npm i mint-ui -S 建议使用npm进行安装 ...