Linked List Cycle II || LeetCode

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode *detectCycle(struct ListNode *head) {
    struct ListNode *fast,*slow;
    bool   flag;
    fast=slow=head,flag=false;
    while(fast&&slow){
        if(fast)fast=fast->next;
        if(fast)fast=fast->next;
        if(slow)slow=slow->next;
        if(fast==slow&&fast!=NULL){
            flag=true;
            break;
        }
    }
    if(flag==false)return NULL;
    slow=head;
    while(slow!=fast){
        slow=slow->next;
        fast=fast->next;
    }
    return slow;
}

  

posted on 2015-06-13 00:16  coderlt  阅读(145)  评论(0编辑  收藏  举报

导航