首页 > 其他分享 >【LeetCode 0141】【链表】【双指针之快慢指针】判断给定单链表是否存在环

【LeetCode 0141】【链表】【双指针之快慢指针】判断给定单链表是否存在环

时间:2024-07-07 13:57:00浏览次数:11  
标签:0141 head list pos fast next 链表 linked 指针

  1. Linked List Cycle

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

There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail’s next pointer is connected to. Note that pos is not passed as a parameter.

Return true* if there is a cycle in the linked list*. Otherwise, return false.

Example 1:

**Input:** head = [3,2,0,-4], pos = 1
**Output:** true
**Explanation:** There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).

Example 2:

**Input:** head = [1,2], pos = 0
**Output:** true
**Explanation:** There is a cycle in the linked list, where the tail connects to the 0th node.

Example 3:

**Input:** head = [1], pos = -1
**Output:** false
**Explanation:** There is no cycle in the linked list.

Constraints:

  • The number of the nodes in the list is in the range [0, 10^4].
  • -10^5 <= Node.val <= 10^5
  • pos is -1 or a valid index in the linked-list.

Follow up: Can you solve it using O(1) (i.e. constant) memory?

Idea
* 如果链表不存在环,则迭代会遍历终止(末尾节点为null)
* 如果存在环,则有且只有一个环,而且只能是在链表尾部形成环
  类似龟兔赛跑,慢指针迟早会赶上快指针,如果存在快指针于慢指针重叠,则说明存在环。
JavaScript Solution
/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

/**
 * @param {ListNode} head
 * @return {boolean}
 */
var hasCycle = function(head) {
    if(!head){
        return false
    }
    let [slow,fast] = [head,head.next]
    while(fast && fast.next){
        if(slow==fast){
            return true
        }
        slow = slow.next
        fast = fast.next.next
    }
    return false
};

标签:0141,head,list,pos,fast,next,链表,linked,指针
From: https://blog.csdn.net/avenccssddnn/article/details/140245330

相关文章

  • 关于指针
    指针是什么一个指针是一个地址是个保存内存地址的整数,对指针类型的定义不会影响变量的内存,但对内存的操作有用内存是个线性的线,周围有很多屋子,每个屋子都一个地址占一个字节,指针就是指这些地址。注意事项int*point=0//这种写法是无效指针可以替换为int*point=NULL;具体使用......
  • 001,函数指针是一种特殊的指针,它指向的是一个函数地址,可以存储函数并作为参数传递,也可
    函数指针是一种特殊的指针001,函数指针是一种特殊的指针,它指向的是一个函数地址,可以存储函数并作为参数传递,也可以用于动态绑定和回调函数文章目录函数指针是一种特殊的指针前言总结前言#include<iostream>usingnamespacestd;intadd(inta,intb){ return......
  • 力扣—盛水最大的容器—双指针
    文章目录题目解析解题思路代码实现题目解析解题思路利用单调性控制其中一个变量,使用双指针控制另一个变量。我们知道S1(面积)=h(高度)*w(宽度)。由于高度的大小是随机的不可控,所以我们可以尝试控制宽度,定义变量left和right分别指向数组第一个元素和最后一个元素......
  • 手绘图系列 01 | 链表到底是什么?
    引言程序=数据结构+算法,在编写代码时,选择一个合适的数据结构是成功的一半。不过,问题是:解决同一个问题有很多种方法。对应到组织数据时,有很多数据结构都可以完成指定的工作。关键在于知道使用哪种数据结构是正确且最高效的。很长一段时间对我来说,链表一直是其中一个比......
  • 相向双指针
    167.两数之和Ⅱ-输入有序数组classSolution{public:vector<int>twoSum(vector<int>&numbers,inttarget){vector<int>ans;intn=numbers.size();intl=0,r=n-1;while(l<r){if((......
  • 代码随想录算法训练营第四天 | 24. 两两交换链表中的节点 、 19.删除链表的倒数第N个
    24.两两交换链表中的节点 题目:.-力扣(LeetCode)思路:这题关键是要每次进行两个结点的操作,并且每次都要保存其前结点,做题思路比较清晰,但是总是处理不好边界问题,总是越界。代码:/***Definitionforsingly-linkedlist.*structListNode{*intval;*List......
  • 代码随想录算法训练营第三天 | 203.移除链表元素 、 707.设计链表 、206.反转链表
    203.移除链表元素题目:.-力扣(LeetCode)思路:主要是通过运用虚拟头节点来统一移除元素的写法。代码:/***Definitionforsingly-linkedlist.*structListNode{*intval;*ListNode*next;*ListNode():val(0),next(nullptr){}*ListNode......
  • 代码随想录刷题day 4 | 两两交换链表中的节点 19.删除链表的倒数第N个节点 面试题
    24.两两交换链表中的节点迭代的版本:最重要的就是要知道循环变量怎么取,对于这道题,我们只需要存储需要交换的两个节点的前一个节点即可,只有当这个节点后面有两个节点时才进入循环,其实把握住这一点之后这题就非常容易了。递归的版本:这道题用递归做简直不要太简单,首先明白递归结束......
  • (十九)指针与迭代器
    前言本来这次我想写关于进制的帖子的,但是感觉指针在为后面的很多内容做铺垫,所以我先把关于指针的帖子写了。况且我知道有很多小伙伴现在没有学习指针,点个赞吧,求求啦!正文指针还记得我们定义普通变量的时候用的是TypVar=Val;,而指针在之前我们讲过,分别出现在这两个帖......
  • 数据结构——(双)链表
    文章目录1. 定义2. 双链表和单链表的区别3.代码示例3.1双链表节点和结构定义3.2初始化双链表3.3 返回双链表的长度3.4 在指定位置插入元素3.5 在末尾插入元素3.6 删除指定位置的元素并返回被删除的元素3.7 删除末尾元素3.8获取指定位置的元素3.9修改指......