LeetCode 142

Linked List Cycle II

Given a linked list, return the node where the cycle begins.
If there is no cycle, return null.

Note: Do not modify the linked list.

Follow up:
Can you solve it without using extra space?

 1 /*************************************************************************
 2     > File Name: LeetCode142.c
 3     > Author: Juntaran
 4     > Mail: [email protected]
 5     > Created Time: Mon 16 May 2016 19:46:12 PM CST
 6  ************************************************************************/
 7
 8 /*************************************************************************
 9
10     Linked List Cycle II
11
12     Given a linked list, return the node where the cycle begins.
13     If there is no cycle, return null.
14
15     Note: Do not modify the linked list.
16
17     Follow up:
18     Can you solve it without using extra space?
19
20  ************************************************************************/
21
22 #include <stdio.h>
23
24 /**
25  * Definition for singly-linked list.
26  * struct ListNode {
27  *     int val;
28  *     struct ListNode *next;
29  * };
30  */
31 struct ListNode *detectCycle(struct ListNode *head)
32 {
33
34     struct ListNode *fast = head;
35     struct ListNode *slow = head;
36
37     int count1 = 0;
38     int count2 = 0;
39     while( slow && fast && fast->next )
40     {
41         fast = fast->next->next;
42         slow = slow->next;
43
44         if( slow == fast )
45         {
46             slow = head;
47             while( slow != fast )
48             {
49                 slow = slow->next;
50                 fast = fast->next;
51             }
52             return slow;
53         }
54     }
55     return NULL;
56 }
时间: 2024-11-13 04:38:37

LeetCode 142的相关文章

LeetCode 142 链表 Linked List Cycle II

LeetCode 142 链表 Linked List Cycle II LeetCode Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexe

Leetcode 142 Linked List Cycle II

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up:Can you solve it without using extra space? 同Leetcode 141 Linked List Cycle 性质:distance from head to 环开始点 == distance from 双指针相遇的点 to 环开始点 证明: 指

Java for LeetCode 142 Linked List Cycle II

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you solve it without using extra space? 解题思路,本题和上题十分类似,但是需要观察出一个规律,参考LeetCode:Linked List Cycle II JAVA实现如下: public ListNode detectCycle(Li

Leetcode 142. Linked List Cycle IIJAVA语言

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note: Do not modify the linked list. 题意:不破坏原链表的情况下判断有没有环,,,,,, /**  * Definition for singly-linked list.  * class ListNode {  *     int val;  *     ListNo

leetcode 142. Linked List Cycle II ----- java

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note: Do not modify the linked list. Follow up:Can you solve it without using extra space? 141题的延伸,求出循环点. 可以用数学方法证明出slow与find相遇的位置一定是所求的点. /** * Definitio

leetcode || 142、Linked List Cycle II

problem: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you solve it without using extra space? Hide Tags Linked List Two Pointers 题意:如果一个单链表存在环形结构,返回环的起点.空间复杂度满足O(1) thinking: (1)上一题使用快

leetcode.142 LinkedList Cycle II

问题描述: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note: Do not modify the linked list. 我的思路: 在Linked List Cycle中, 我们使用了快慢指针来判断链表是否存在环.而在一开始,我天真的以为,快慢指针相遇的地方便是循环入口,虽然有这种可能性,但是也存在相遇点不是入口的可能性.我们要找更一般的表

LeetCode 142——环形链表 II

1. 题目 2. 解答 2.1 方法 1 定义快慢两个指针,慢指针每次前进一步,快指针每次前进两步,若链表有环,则快慢指针一定会相遇. 当快慢指针相遇时,我们让慢指针指向头节点,快指针不变,然后每次快慢指针都前进一步,当两个指针再次相遇时,两个指针所指向的节点就是入环节点. 将链表的环向后展开,如上图所示,假设第一次相遇时慢指针走过了 a 个节点,即图中 s 节点,可知此时快指针指向同一个节点,即图中的 f 节点. 然后,假设慢指针从链表头到入环节点共有 b 个节点,快指针从快慢指针相遇节点到入

Leetcode 142.环形链表II

环形链表II 给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 说明:不允许修改给定的链表. 进阶:你是否可以不用额外空间解决此题? 链表头是X,环的第一个节点是Y,slow和fast第一次的交点是Z.各段的长度分别是a,b,c,如图所示.环的长度是L. 第一次相遇时slow走过的距离:a+b,fast走过的距离:a+b+c+b. 因为fast的速度是slow的两倍,所以fast走的距离是slow的两倍,有 2(a+b) = a+b+c+b,可以得到a=c(这个结论