码迷,mamicode.com
首页 > 其他好文 > 详细

Linked List Cycle II || LeetCode

时间:2015-06-13 06:19:10      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

/**
 * 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;
}

  

Linked List Cycle II || LeetCode

标签:

原文地址:http://www.cnblogs.com/ProtectedDream/p/4572854.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!