[算法笔记]-环形链表Ⅱ-解题方法
原题链接:https://leetcode-cn.com/problems/linked-list-cycle-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. If pos
is -1
, then there is no cycle in the linked list.
Note: Do not modify the linked list.
Example 1:
Input: head = [3,2,0,-4], pos = 1 Output: tail connects to node index 1 Explanation: There is a cycle in the linked list, where tail connects to the second node.
Example 2:
Input: head = [1,2], pos = 0 Output: tail connects to node index 0 Explanation: There is a cycle in the linked list, where tail connects to the first node.
Example 3:
Input: head = [1], pos = -1 Output: no cycle Explanation: There is no cycle in the linked list.
Follow-up:
Can you solve it without using extra space?
引用官方题解的配图:
解法思路:
1. 快慢指针
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {public:ListNode *detectCycle(ListNode *head) {if(!head)return NULL;ListNode *fast = head, *slow = head;bool has_cycle = false;while(fast && fast->next){fast = fast->next->next;slow = slow->next;if(fast == slow){has_cycle = true;break;}}if(has_cycle){ListNode *cycle_start = head;while(cycle_start!=slow){cycle_start = cycle_start->next;slow = slow->next;}return cycle_start;}else{return NULL;}}
};
2. 哈希表
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {public:ListNode *detectCycle(ListNode *head) {unordered_map <ListNode *, int> m;ListNode *p = head;while(p){if(m[p]) return p;m[p]++;p = p->next;} return NULL;}
};
[算法笔记]-环形链表Ⅱ-解题方法相关推荐
- php环形链表,PHP环形链表实现方法示例
本文实例讲述了PHP环形链表实现方法.分享给大家供大家参考,具体如下: 环形链表是一种链式存储结构,类似于单链表.区别是环形链表的尾节点指向头节点. 从而形成一个环, 环形链表是一种非常灵活的存储结构 ...
- leetcode算法题--环形链表 II★
原题链接:https://leetcode-cn.com/problems/linked-list-cycle-ii/ 1.map ListNode *detectCycle(ListNode *he ...
- 算法笔记一 链表的翻转(C++)
一 两两交换链表的节点 问题具体见Leecode 算法练习24题 1.1问题分析 1.1.1非递归法 对于非递归的方法我们主要分为四个步骤 1.新建一个节点tamp,让这个节点的next指向头节点 2 ...
- 算法笔记-经典链表操作案例
单链表反转 链表中环的检测 两个有序的链表合并 删除链表倒数第 n 个结点 求链表的中间结点 <?php /*** User: lide01* Date: 2018/10/9 14:06* De ...
- 算法笔记-判断链表保存的字符串是否是回文
<?php/*** 单链表节点** Class SingleLinkedListNode** @package Algo_06*/ class SingleLinkedListNode {/** ...
- Leetcode 141. 环形链表 解题思路及C++实现
解题思路: 定义快慢两个指针,当指针所指节点是同一个时,说明出现了环. /*** Definition for singly-linked list.* struct ListNode {* int ...
- leetcode算法题--环形链表
原题链接:https://leetcode-cn.com/problems/linked-list-cycle/ 快慢指针 bool hasCycle(ListNode *head) {ListNod ...
- 快慢指针:141. 环形链表(判断是否存在环路)
题目描述 给定一个链表,判断链表中是否有环 题目链接 141. 环形链表 解题思路 使用快慢指针(Floyd判圈算法):从链表的头部设置两个指针,p1的步长为1, p2的步长为2,同时向前走,如果p1 ...
- leetcode206.反转链表 解题思路(简单)
难度: 简单 题目:给你单链表的头结点head,请你反转链表,并返回反转后的链表. 示例1: 示例2: 示例3: 题目类型:数据结构链表 解题方法:双指针迭代法 解题思路: 1.申请两个新指针,分别指 ...
最新文章
- oracle vm concat指定分隔符,重写Oracle的wm_concat函数,自定义分隔符、排序
- C++ volatile关键字说明
- Linux学习134 Unit 8
- css scale 元素不放大,列元素上的CSS 3动画“transform:scale”对chrome不起作用
- IndexedDB:浏览器里内置的数据库
- 第12章[12.2] Ext JS可编辑列Grid的全场景开发
- 关于国庆假期延长的通知
- easyUI的常用API
- V4L2- Memory
- 支付宝小程序悬浮按钮
- python common很久不用我都忘了一些函数了,给boss添麻烦了
- SDNU_ACM_ICPC_2019_Winter_Practice_9th题解
- Vue项目关闭格式检查命令
- V型测试,W型测试和H型测试
- vue实现数据无缝循环滚动
- 个人作业-Week3
- 怎么修改游戏服务器地址,如何修改游戏服务器ip地址
- 确定anaconda与安装的python对应版本的方法
- Android 利用广播实现短信的自动转发
- C语言实现OOP——轻量级的面向对象 C 语言编程框架 LW_OOPC 介绍(三)
热门文章
- 30分钟正则表达式入门
- mysql accountlevel1_mysql---修改表结构
- [JSP暑假实训] 一.MyEclipse安装及JAVA基础知识
- 【数据结构与算法】之深入解析“最好买卖股票的时机含冷冻期”的求解思路与算法示例
- RxSwift之常用高阶函数(操作符Operator)的说明和使用
- iOS之深入定制基于PLeakSniffer和MLeaksFinder的内存泄漏检测工具
- 2 0 2 0 年 第 十 一 届 蓝 桥 杯 - 省赛 - Python大学组 - A. 门牌制作
- 《每日一题》48. Rotate Image 旋转图像
- 战疫期,钉钉如何扛起暴增百倍的流量?
- html jquery 不能自动完成,在jQuery UI自动完成中使用HTML