linked-list-cycle——链表、判断是否循环链表、快慢指针

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

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

若在while开始时判断fast==slow,会出现误判,即第一次循环时fast必定等于slow

 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 Solution {
10 public:
11     bool hasCycle(ListNode *head) {
12         if(head==NULL)
13             return false;
14         ListNode *slow=head,*fast=head;
15         while(fast!=NULL&&fast->next!=NULL){
16
17             fast=fast->next->next;
18             slow=slow->next;
19             if(fast==slow)
20                 return true;
21
22         }
23         return false;
24     }
25 };
时间: 2024-07-30 17:05:20

linked-list-cycle——链表、判断是否循环链表、快慢指针的相关文章

Leetcode:Linked List Cycle 链表是否存在环

Linked List Cycle: Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? 解题分析: 大致思想就是设置两个指针,一个指针每次走两步,一个指针每次走一步,如果这两个指针碰头了,那么一定就存在环 可以类比两个人在环形操场跑步,同时出发,一个跑得快,一个跑得慢,如果跑得快的人追上跑得慢的人,那么跑得快的人相当于多跑了一整

[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 141. Linked List Cycle 链表

141. Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? 题目大意: 判断一个单链表是否存在环. 思路: 采用快慢指针来处理. 代码如下: /**  * Definition for singly-linked list.  * struct ListNode {  *     int va

141. Linked List Cycle(判断l链表是否有环)(leetcode)

Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in

【LeetCode每天一题】Linked List Cycle II(循环链表II)

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-indexed) in the linked list where tail connects to.

快慢指针原理--快速找到未知长度单链表的中间节点

package com.java.dataStruct; //节点类 public class Node<E> { E item; Node next; public Node(){ } public Node(E element){ this.item = element; } public Node(E element, Node next){ this.item = element; this.next = next; } } Node p1,r1; Node L1 = new Node

判断链表是否有环及环入口点的求法(Linked List Cycle II )

分为两步 第一步 还是利用快慢指针,如果有环的话在利用快慢指针终会相会于一个节点. 第二步.然后从这节点出发每次出发走一步,同时从根节点出发每次出发也走一步则他们两个指针相遇的地方就是环的入口. 第一步好解释那么第二步是为什么呢? 网上有很多解法大都是从数学的角度来分析,有公式也有推算很不直观,我从图形的角度来看的话就相对理解起来简单很多. 将图摊开成一条线,假设我们有环而且假设快指针就多走了一圈就与慢指针相遇了(多走n圈其实也是一样的,画出图来也不难理解,只是画起来麻烦索性就以一圈来代表) 红

[leetcode]141. Linked List Cycle判断链表是否有环

Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? 题意: 给定一个链表,判断是否有环 思路: 快慢指针 若有环,则快慢指针一定会在某个节点相遇(此处省略证明) 代码: 1 public class Solution { 2 public boolean hasCycle(ListNode head) { 3 ListNode f

Linked List Cycle 判断一个链表是否存在回路(循环)

Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? 注意,链表循环并不是尾指针和头指针相同,可能是在中间某一段形成一个环路,所以不能只判断元素和第一个元素是否存在重合 先设置两个指针p_fast和p_slow.从头开始遍历链表,p_fast每次走两个节点,而p_slow每次走一个节点,若存在循环,这两个指针必定重合: 1 /**

LeetCode 141. Linked List Cycle(判断链表是否有环)

题意:判断链表是否有环. 分析:快慢指针. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: bool hasCycle(ListNode *head) { ListNode* fast = head; ListNode