Linked List Cycle leetcode java (链表检测环)

题目:

Given a linked list, determine if it has a cycle in it.

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

题解:

这道题连带着II是很经典的,在看CC150时候,纠结这个问题纠结了很久。在读了很多网上资料还有书的讲解以及和别人讨论之后,对这个专题终于明白了。

这一问只需要判断链表是否有环。

当链表没有环时是很好判断的,让一个指针一直往后走,遇见null了自然就没有环。

而如何判断有环,那么就需要引入Faster和Slower的概念了(也是一种双指针方法)。顾名思义,同个时间Faster走的比Slower走的多。一般来说,Slower每次走一步,Faster每次走2步(通常这个概念可以判断链表中间点)。在这里,Faster和Slower同时从起点遍历链表,如果有环那么Slower和Faster肯定会相遇。

为什么他俩肯定能相遇呢?万一一个把一个超了但是没相遇咋办?

直觉和生活经验告诉我,他俩肯定能相遇,比如在操场跑圈,一个快的一个慢的同时开始跑,一直跑,快的肯定能跟慢的相遇。不过有更严谨的说法就更有说服力了。

下面我就引用一下CC150里面外加我的完善来说明怎么证明的这个问题:

假设Faster确实把Slower超了而且他俩还没相遇(类似Faster一下迈了2步,Slower一下迈了一步,Faster超了Slower,但是俩人并没遇上)。那么就假设Faster现在在 i+1 位置而Slower在 i 位置。那么前一时刻,Slower肯定是在 i-1 位置,而Faster肯定在(i+1)-2位置,所以前一时刻,俩人都在 i-1 位置,相遇了。

还有一种情况就是Faster在i+2位置而slower在i位置,那么前一时刻,Faster在i位置,而Slower在 i-1位置。这样问题又回归到上面那种情况了(再往前一时刻,Faster在i-2位置,Slower在i-1-1位置,相遇)。

所以,这就证明Runner和Faster在有环的链表中肯定会相遇。

代码工作就很简单了,如下:

1     public boolean hasCycle(ListNode head) {
 2         if(head == null || head.next == null)
 3             return false;
 4         
 5         ListNode Faster = head, Slower = head;
 6         
 7         while(Faster.next!=null && Faster.next.next!=null){
 8             Slower = Slower.next;
 9             Faster = Faster.next.next;
10             
11             if(Faster == Slower)
12                 return true;
13         }
14         return false;
15     }

Linked List Cycle leetcode java (链表检测环)

时间: 2024-08-24 10:52:16

Linked List Cycle leetcode java (链表检测环)的相关文章

142 Linked List Cycle II(如果链表有环,找到入口结点)

题目意思:如果有环,返回入口结点 思路:先判断有没环,再计算环的结点数,然后p1指向头,p2往后移结点次数,p1.p2相遇为入口结点 ps:还是利用指针间距这个思路 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Sol

Linked List Cycle leetcode II java (寻找链表环的入口)

题目: 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? 题解: 这个连同I都是很经典的题啦,刷CC150时候就折磨了半天. 其实就推几个递推公式就好..首先看图(图引用自CC150): 从链表起始处到环入口长度为:a,从环入口到Faster和Sl

Reverse Linked List II leetcode java

题目: Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n

小程序 - 链表检测环/链表是否交叉 等

链表检测环 int cycleExists(Node *head) { Node *fast, *slow; if (head == NULL) return 0; for (slow = head, fast = head->next; fast && fast->next; fast = fast->next->next, slow = slow->next) if (slow == fast) return 1; return 0; } 链表是否交叉 i

[Leetcode] Linked list cycle ii 判断链表是否有环

Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follow up:Can you solve it without using extra space? 题意:给定链表,若是有环,则返回环开始的节点,没有则返回NULL 思路:题目分两步走,第一.判断是否有环,第二若是有,找到环的起始点.关于第一点,可以参考之前的博客 Linked list cycle.

[LeetCode]93. 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? Subscribe to see which companies asked this question 解法:这道题是题目L

142 Linked List Cycle II 环形链表 II

给一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null.说明:不应修改给定的链表.补充:你是否可以不用额外空间解决此题?详见:https://leetcode.com/problems/linked-list-cycle-ii/description/ /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val

[LeetCode] 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? 这个求单链表中的环的起始点是之前那个判断单链表中是否有环的延伸,可参见我之前的一篇文章 (http://www.cnblogs.com/grandyang/p/4137187.html). 还是要设

LeetCode Linked List Cycle II 单链表环2 (找循环起点)

题意:给一个单链表,若其有环,返回环的开始处指针,若无环返回NULL. 思路: (1)依然用两个指针的追赶来判断是否有环.在确定有环了之后,指针1跑的路程是指针2的一半,而且他们曾经跑过一段重叠的路(即1跑过,2也跑过),就是那段(环开始处,相遇处),那么指针2开始到环开始处的距离与head到指针相遇处是等长的喔~,那么再跑一次每次一步的就必定会相遇啦.画个图图好方便看~ (2)其实还有另一个直观的思路,就是指针1和2相遇后,p指向他们的next,在他们相遇处的next给置空,再跑一遍那个“找两