zoukankan      html  css  js  c++  java
  • 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?

    Code:

    class Solution {
    public:
        ListNode *detectCycle(ListNode *head) {
            ListNode *p=head;
            ListNode *cur=head;
            while(p){
                if(p->next)
                    p=p->next->next;
                else
                    return NULL;
                cur=cur->next;
                if(p==cur)
                    return findSpot(head,p);
            }
            return NULL;
        }
    ListNode
    *findSpot(ListNode *head,ListNode *p){ while(head!=p){ head=head->next; p=p->next; } return head; } };
  • 相关阅读:
    RTP/RTSP编程
    makefile
    VS 2010内存泄漏检测
    Linux Shell中捕获CTRL+C
    const
    Hdu 5344
    Hdu5762
    CF1200C
    CF1200B
    CF1200A
  • 原文地址:https://www.cnblogs.com/winscoder/p/3400682.html
Copyright © 2011-2022 走看看