Linked List Cycle II

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?

和问题一Linked List Cycle几乎一样。如果用我的之前的解法的话,可以很小修改就可以实现这道算法了。但是如果问题一用优化了的解法的话,那么就不适用于这里了。下面是我给出的解法,可以看得出,这里需要修改很小地方就可以了。

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public: bool find(ListNode *head, ListNode *testpNode)
{
ListNode *p = head;
while (p != testpNode->next)
{
if(p == testpNode)
return false;
p = p->next;
}
return true;
} ListNode *detectCycle(ListNode *head) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if(head == NULL)
return false; ListNode *cur = head;
while(cur != NULL)
{
if(find(head, cur))
return cur->next;
cur = cur->next;
}
return NULL;
}
};

然后转一下下面那位朋友的博客,他的解法很优化,不过只适合第一个LeetCode Linked List Cycle问题,而不适合这里。值得学习学习,一起贴在这里了。

http://blog.csdn.net/doc_sgl/article/details/13614853

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool hasCycle(ListNode *head) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
ListNode* pfast = head;
ListNode* pslow = head;
do{
if(pfast!=NULL)
pfast=pfast->next;
if(pfast!=NULL)
pfast=pfast->next;
if(pfast==NULL)
return false;
pslow = pslow->next;
}while(pfast != pslow);
return true;
}
};

LeetCode Linked List Cycle II 和I 通用算法和优化算法的更多相关文章

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

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

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

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

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

  4. [LeetCode] Linked List Cycle II, Solution

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

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

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

  6. LeetCode——Linked List Cycle II

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

  7. Leetcode Linked List Cycle II

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

  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 单链表环2 (找循环起点)

    题意:给一个单链表,若其有环,返回环的开始处指针,若无环返回NULL. 思路: (1)依然用两个指针的追赶来判断是否有环.在确定有环了之后,指针1跑的路程是指针2的一半,而且他们曾经跑过一段重叠的路( ...

随机推荐

  1. 完美.PCK文件不完全详解

    building.pck:建筑物 configs.pck:配置文件interface.pck:玩过魔兽的都晓得这个是什么~interface顾名思义·facedata.pck,人物脸型,细节.不要改, ...

  2. WebLogic清理缓存

    如果发布到weblogic的工程,登录发现还是原来的代码错误,可尝试清理weblogic缓存: 1.在weblogic控制台中停止应用,删除部署的工程 2.登录weblogic服务器,删除以下目录中的 ...

  3. C/C++中const关键字

    http://blog.csdn.net/xdrt81y/article/details/24333335 今天在做一个趋势笔试题的时候,才让我有了系统把const关键字好好总结一下的冲动,因为这个关 ...

  4. Struts2学习笔记——Struts2与Spring整合

      Struts2与Spring整合后,可以使用Spring的配置文件applicationContext.xml来描述依赖关系,在Struts2的配置文件struts.xml来使用Spring创建的 ...

  5. SimpleAdapter和AutoCompleteTextView配合使用

     修改后成为这样→    package com.kale.autocompletetextview; import java.util.ArrayList; import java.util.Has ...

  6. js如何判断用户是在pc端和还是移动端访问

    js如何判断用户是在pc端和还是移动端访问 来源:A5技术交流 作者:wofa 时间:2014-04-25收藏本页 最近一直在忙我们团队的项目“咖啡之翼”,在这个项目中,我们为移动平台提供了一个优秀的 ...

  7. 利用rest-framework实现django应用的分页功能

    自定义分页的类,继承 PageNumberPagination class StandardResultsSetPagination(PageNumberPagination): page_size ...

  8. Grid++Report

    ylbtech-Miscellaneos:Grid++Report 1. 关于Grid++Report返回顶部 Grid++Report 可用于开发桌面C/S报表与WEB报表(B/S报表),C/S报表 ...

  9. 数据库实例: STOREBOOK > 用户 > 编辑 用户: SYS

    ylbtech-Oracle:数据库实例: 数据库实例: STOREBOOK  >  用户  >  编辑 用户: SYS 编辑 用户: SYS 1. 一般信息返回顶部 1.1, 1.2, ...

  10. Go语言之进阶篇爬捧腹网

    1.爬捧腹网 网页规律: https://www.pengfu.com/xiaohua_1.html   下一页 +1 https://www.pengfu.com/xiaohua_2.html 主页 ...