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?

本来不是很难的一题,提交了n遍,一直运行时错误,为什么呢?因为一个指针判断的问题,由于要向后移动两个指针,因为p->next也需要判断,而不能仅仅判断p,不然p=p->next->next会有问题。

 

C++代码实现:

#include<iostream>
#include<new>
using namespace std;

#include<iostream>
#include<new>
using namespace std;

//Definition for singly-linked list.
struct ListNode
{
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};
class Solution
{
public:
    ListNode *detectCycle(ListNode *head)
    {
        //创建一个环至少需要2个结点
        if(head==NULL||head->next==NULL)
            return NULL;
        ListNode *p=head;
        ListNode *pre=head;
        while(p&&p->next)
        {
            p=p->next->next;
            pre=pre->next;
            if(pre==p)
                break;
        }
        if(p==NULL||p->next==NULL)
        {
            return NULL;
        }
        p=head;
        while(pre!=p)
        {
            //判断要放在前面,因为从头结点开始就是一个环时,两个指针会相遇在头结点,此时并不需要指针后移
            //if(pre==p)
             //   return pre;
            pre=pre->next;
            p=p->next;
        }
        return p;
    }
    void createList(ListNode *&head)
    {
        ListNode *p=NULL;
        ListNode *cycle=NULL;
        int i=0;
        int arr[10]= {10,9,8,7,6,5,4,3,2,1};
        for(i=0; i<10; i++)
        {
            if(head==NULL)
            {
                head=new ListNode(arr[i]);
                if(head==NULL)
                    return;
                //为了创建一个环,记录尾指针
                cycle=head;
            }
            else
            {
                p=new ListNode(arr[i]);
                p->next=head;
                head=p;
            }
        }
        cycle->next=head->next->next->next;
    }
};

int main()
{
    Solution s;
    ListNode *L=NULL;
    s.createList(L);
    ListNode *head=L;
    L=s.detectCycle(L);
    head=L;
    while(L)
    {
        cout<<L->val<<" ";
        L=L->next;
        if(L==head)
            break;
    }
}

运行结果:

时间: 2024-09-01 20:50:06

Linked List Cycle II的相关文章

[LeetCode]142.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? 分析: 首先使用快慢指针技巧,如果fast指针和slow指针相遇,则说明链表存在环路.当fast与slow相遇时,slow肯定没有遍历完链表,而fast已经在环内循环了n圈了(1<=n)设s

[LeetCode 第11题] -- Linked List Cycle II

题目链接: Linked List Cycle II 题目意思: 给定一个链表,如果链表有环求出环的起点,否则返回NULL 解题思路:      1. 判断链表是否有环: 两个指针,一个一次走一步,一个一次走两步,如果指针相遇说明有环,否则无环.     2. 如果有环的情况下,我们可以画个图(图片来自网络)                   假设两个指针在z点相遇.则          a. 指针1走过路程为a + b:指针2走过的路程为 a+b+c+b          b. 因为指针2的

[LeetCode] 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? 解题思路 设链表长度为n,头结点与循环节点之间的长度为k.定义两个指针slow和fast,slow每次走一步,fast每次走两步.当两个指针相遇时,有: fast = slow * 2 fast -

[LeetCode]141.Linked List Cycle

[题目] Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? [题意] 给定一个链表,确定它是否包含一个环. [分析] 最容易想到的方法是,用一个哈希表 unordered_map<int, bool> visited,记录每个元素是否被访问过,一旦出现某个元素被重复访问,说明存在环. 空间复杂度 O(n),时间复杂度 O(N

[LeetCode 第10题] -- Linked List Cycle

题目链接: 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 hasCycle(ListNode *head); }; b

[LeetCode] Linked List Cycle

Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 解题思路 使用快慢两个指针,如果快的指针赶上了慢的,则说明存在回路. 实现代码 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNod

LeetCode总结【转】

转自:http://blog.csdn.net/lanxu_yy/article/details/17848219 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近完成了www.leetcode.com的online judge中151道算法题目.除各个题目有特殊巧妙的解法以外,大部分题目都是经典的算法或者数据结构,因此做了如下小结,具体的解题思路可以搜索我的博客:LeetCode题解 题目 算法 数据结构 注意事项 Clone Graph BFS 哈希表 Word Ladder II

careercup-链表 2.6

2.6 给定一个有环链表,实现一个算法返回环路的开头结点. 类似leetcode中 Linked List Cycle II C++实现代码: #include<iostream> #include<new> using namespace std; struct ListNode { int val; ListNode *next; ListNode(int x):val(x),next(NULL) {} }; void createList(ListNode *&L)

LeetCode All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能