首页 > 其他 > 详细

LeetCode 142 Linked List Cycle II

时间:2018-11-20 14:26:56      阅读:149      评论:0      收藏:0      [点我收藏+]

LeetCode 142

每遍历一个点,都要判断起点到这个点的距离,和启动点到这个点的next的距离。再比较一下就可以了。

class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        
        ListNode* iter = head;
        
        int dis1=0;
        int dis2;
      
        while(iter!=NULL)
        {
            dis1++;
            iter = iter->next;
            
            ListNode* temp = iter;
            ListNode* iter2 = head;
            dis2=1;
            while(iter2!=NULL&&iter2!=temp)
            {
                dis2++;
                iter2=iter2->next;
            }
            
            if(dis1>=dis2)
                return iter;
        }
        return NULL;  
    }

LeetCode 142 Linked List Cycle II

原文:https://www.cnblogs.com/dacc123/p/9988473.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!