【LeetCode】Linked List Cycle I&II

原创 2015年07月11日 00:48:19

Problem

这里写图片描述

Code

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
bool hasCycle(struct ListNode *head) {
    if(head==NULL)
        return false;;
    struct ListNode *faster;
    struct ListNode *slower;
    faster = head->next;
    slower = head;
    while(true){
        if(faster==NULL)
            break;
        if(faster->next==NULL)
            break;
        if(faster==slower)
            return true;
        slower = slower->next;
        faster = faster->next->next;
    }
    return false;;
}
 /*似乎不能改变节点的结构,否则释放的时候会RTE
bool hasCycle(struct ListNode *head) {
    struct ListNode *tmp;
    while(head!=NULL){
        if(head==head->next)
            return false;
        tmp = head;
        head = head->next;
        tmp->next = tmp;
    }
    return true;
}
*/

Comment

This story tells me,the slower will always be overrun by the faster.


Problem

这里写图片描述

Code

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode *detectCycle(struct ListNode *head) {
    struct ListNode *faster = head;
    struct ListNode *slower = head;
    struct ListNode *entry = head;
    while(faster!=NULL && faster->next!=NULL){
        faster = faster->next->next;
        slower = slower->next;
        if(slower==faster){
            while(slower!=entry){
                slower = slower->next;
                entry = entry->next;
            }
            return entry;
        }
    }
    return NULL;
}

 /*取巧的方法,如果测试集够大,那么这个方法还是错误的 : )
struct ListNode *detectCycle(struct ListNode *head) {
    while(head!=NULL){
        if(head->val == (int)head)
            return head;
        else
            head->val = (int)head;
        head = head->next;
    }
    return NULL;
}
*/

Alogrithm Description

Step 1: Determine whether there is a cycle

1.1) Using a slow pointer that move forward 1 step each time

1.2) Using a fast pointer that move forward 2 steps each time

1.3) If the slow pointer and fast pointer both point to the same location after several moving steps, there is a cycle;

1.4) Otherwise, if (fast->next == NULL || fast->next->next == NULL), there has no cycle.

Step 2: If there is a cycle, return the entry location of the cycle

2.1) L1 is defined as the distance between the head point and entry point

2.2) L2 is defined as the distance between the entry point and the meeting point

2.3) C is defined as the length of the cycle

2.4) n is defined as the travel times of the fast pointer around the cycle When the first encounter of the slow pointer and the fast pointer

According to the definition of L1, L2 and C, we can obtain:

the total distance of the slow pointer traveled when encounter is L1 + L2

the total distance of the fast pointer traveled when encounter is L1 + L2 + n * C

Because the total distance the fast pointer traveled is twice as the slow pointer, Thus:

2 * (L1+L2) = L1 + L2 + n * C => L1 + L2 = n * C => L1 = (n - 1)* C + (C - L2)

It can be concluded that the distance between the head location and entry location is equal to the distance between the meeting location and the entry location along the direction of forward movement.

So, when the slow pointer and the fast pointer encounter in the cycle, we can define a pointer “entry” that point to the head, this “entry” pointer moves one step each time so as the slow pointer. When this “entry” pointer and the slow pointer both point to the same location, this location is the node where the cycle begins.

版权声明:Pain is just in your mind.

相关文章推荐

LeetCode :: Linked List Cycle I and II

I.Given a linked list, determine if it has a cycle in it. II. Given a linked list, return the nod...

LeetCode: Linked List Cycle I & II

推荐参照:Leetcode题目难度等级及面试频率总结     这是最近我总结的一些关于链表的一些操作合集,包括测试代码  从今天开始,主要总结一些关于数据结构——链表的一些基本操作和相关LeetCod...

LeetCode - Linked List Cycle I &II

转载:http://www.cnblogs.com/hiddenfox/p/3408931.html 题目要求Linked List CycleGiven a linked list, determi...

LeetCode Linked List Cycle II 和I 通用算法和优化算法

和问题一Linked List Cycle几乎一样。如果用我的之前的解法的话,可以很小修改就可以实现这道算法了。但是如果问题一用优化了的解法的话,那么就不适用于这里了。下面是我给出的解法,可以看得出,...
  • kenden23
  • kenden23
  • 2013年11月01日 07:50
  • 13726

leetcode Linked List Cycle I II

今天抽空做了几题,突然发现下面的题都过去很久了,发现这两天更新了两个链表,就写这两个链表吧。 题目1: 题目就是判断一个链表是否有环。。要求不增加额外空间。如果以前没有做过可能会卡,这个题因为我以前见...

Linked List Cycle I&&II

Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without usin...

142. Linked List Cycle I&II

Linked List Cycle I 只是单纯地确定是否有cycle,可以使用两个指针(fast和slow),fast每次走两步,slow每次走一步。若最后有相交的节点,则有环;若fast节点为nu...

LeetCode Linked List Cycle II(找到带环单向链表的环起始位置)

题目要求: Given a linked list, return the node where the cycle begins. If there is no cycle, return...
  • lqcsp
  • lqcsp
  • 2014年04月06日 11:52
  • 578

【LeetCode】Linked List Cycle II

参考链接 http://www.cnblogs.com/x1957/p/3406448.html 题目描述 Linked List Cycle II   Give...

leetcode:Linked List Cycle II

COPY FROM: 比I麻烦点的就是找到循环开始点TAT I只是判断是否循环。要求不使用额外空间(不然hash就可以了 按I的思路,我们又慢指针S和快指针F。。。F走两...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:【LeetCode】Linked List Cycle I&II
举报原因:
原因补充:

(最多只允许输入30个字)