公司和学校事情比较多,隔了好几天没刷题,今天继续刷起来

题目

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 实现的更多相关文章

  1. leetcode Linked List Cycle II python

    # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...

  2. 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 ...

  3. 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 ...

  4. [LeetCode] Linked List Cycle II 单链表中的环之二

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  5. [Leetcode] Linked list cycle ii 判断链表是否有环

    Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follo ...

  6. [LeetCode] Linked List Cycle II, Solution

    Question : Given a linked list, return the node where the cycle begins. If there is no cycle, return ...

  7. [LeetCode]Linked List Cycle II解法学习

    问题描述如下: Given a linked list, return the node where the cycle begins. If there is no cycle, return nu ...

  8. LeetCode——Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  9. Leetcode Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  10. [LeetCode] Linked List Cycle II 链表环起始位置

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

随机推荐

  1. GoDaddy虚拟主机创建FTP 图文流程

    有了ftp各种操作就方便多了,也不用通过网页的控制面板来修改代码了 狗爹linux虚拟主机创建FTP 1. 通过虚拟主机管理界面,进入cPanel控制面板 2. 进入FTP管理页面 3. 填写账号.密 ...

  2. 如何下载Oracle E-Business Suite (12.2.6) for Microsoft Windows x64 (64-bit)

    下载地址:https://edelivery.oracle.com/ 使用您的 Oracle 账户进行登录.如果您没有该账户, 请注册 Oracle 账户.     Oracle Software D ...

  3. 《Unity預計算即時GI》笔记:三、Clusters和总结

    Clusters 叢集,透過修改叢集(Clusters)也是一個降低Unity預計算流程所需要執行的工作數量的好方法.降低叢集數量也能提高執行時的效能. 當採用PRGI來計算場景光照時,Unity會簡 ...

  4. chrome中清除dns缓存

    chrome中清除dns缓存 http://rss.code-mire.com/item/1005.htm web开发经常要做各种host绑定的切换,firefox下有个DNS Flusher插件,但 ...

  5. cms-帖子幻灯图片上传

    package com.open1111.controller.admin; import java.io.File;import java.util.Date;import java.util.Ha ...

  6. pat甲级1044二分查找

    1044 Shopping in Mars(25 分) Shopping in Mars is quite a different experience. The Mars people pay by ...

  7. js 对象字面量

    对象字面量的输出方式以及定义好处 1.对象字面量的输出方式有两种:传统的'.' 例如:box.name 以及数组方式,只不过用数组方式输出时,方括号里面要用引号括起来 例如:box['name'] v ...

  8. spark 之knn算法

    好长时间忙的没写博客了.看到有人问spark的knn,想着做推荐入门总用的knn算法,顺便写篇博客. 作者:R星月  http://www.cnblogs.com/rxingyue/p/6182526 ...

  9. 2018.2.6 JS-判断用户浏览器

    JS-判断用户浏览器 在判断用户使用的浏览器是否为PC还是移动设备,有时候项目中需要用到.可在需要的项目中当全局方法来使用. 判断代码 function getMoblieDevice(window) ...

  10. Bootstrap历练实例:popover插件中的方法

    方法 下面是一些弹出框(Popover)插件中有用的方法: 方法 描述 实例 Options: .popover(options) 向元素集合附加弹出框句柄. $().popover(options) ...