【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.

leetcode141: Linked List Cycle

Given a linked list, determine if it has a cycle in it.Follow up: Can you solve it without using ex...
  • u010111016
  • u010111016
  • 2016年05月17日 10:30
  • 1522

【LeetCode】141 Linked List Cycle (java实现)

Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without usi...
  • styshoo
  • styshoo
  • 2015年09月02日 23:34
  • 1140

(Leetcode 142)Linked List Cycle (II) (快慢指针详解)

Leetcode141题和142题很相似,都涉及到了快慢指针的算法。快慢指针就是设置两个指针,一个快指针,一个慢指针来达到解题的目的。下面分为以下几个模块来讲解: 解第141题解第142题为什么快...
  • willduan1
  • willduan1
  • 2016年03月20日 21:05
  • 8244

leetcode:142. Linked List Cycle II(Java)解答

转载请注明出处:z_zhaojun的博客 原文地址:http://blog.csdn.net/u012975705 题目地址:https://leetcode.com/problems/link...
  • u012975705
  • u012975705
  • 2015年12月27日 16:36
  • 2189

[Leetcode-142] Linked List Cycle II(链表有环详细分析)

环的长度是多少? 如何找到环中第一个节点(即Linked List Cycle II)? 如何将有环的链表变成单链表(解除环)? 如何判断两个单链表是否有交点?如何找到第一个相交的节点?...
  • xy010902100449
  • xy010902100449
  • 2015年10月09日 09:33
  • 1400

LeetCode 141 — Linked List Cycle(C++ Java Python)

题目:http://oj.leetcode.com/problems/linked-list-cycle/ Given a linked list, determine if it has a cyc...
  • dragon_dream
  • dragon_dream
  • 2014年02月27日 21:26
  • 2274

LeetCode (27) Linked List Cycle (判断cycle存在、寻找cycle入口节点)

判断cycle存在Given a linked list, determine if it has a cycle in it.Follow up: Can you solve it without ...
  • angelazy
  • angelazy
  • 2015年04月28日 14:36
  • 1157

Linked List Cycle -- LeetCode

原题链接: http://oj.leetcode.com/problems/linked-list-cycle/  这道题是cc150里面的题目,算是链表里面比较经典的题目。 我们先讲一下比较直接容易...
  • linhuanmars
  • linhuanmars
  • 2014年03月19日 04:54
  • 6587

LeetCode(141)Linked List Cycle

题目如下: Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without us...
  • feliciafay
  • feliciafay
  • 2014年01月10日 00:46
  • 2495

Linked List Cycle I&II

Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without us...
  • awawfwfw
  • awawfwfw
  • 2016年10月29日 02:30
  • 115
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:【LeetCode】Linked List Cycle I&II
举报原因:
原因补充:

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