LeetCode141 Linked List Cycle. LeetCode142 Linked List Cycle II
链表相关题
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? (Easy)
分析:
采用快慢指针,一个走两步,一个走一步,快得能追上慢的说明有环,走到nullptr还没有相遇说明没有环。
代码:
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 0; 14 } 15 ListNode* slow = head; 16 ListNode* fast = head; 17 while (fast != nullptr && fast->next != nullptr) { 18 slow = slow->next; 19 fast = fast->next->next; 20 if (slow == fast) { 21 return true; 22 } 23 } 24 return false; 25 } 26 };
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?(Medium)
分析:
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 ListNode *detectCycle(ListNode *head) { 12 if(head == nullptr) { 13 return 0; 14 } 15 ListNode* slow = head; 16 ListNode* fast = head; 17 while (fast != nullptr && fast->next != nullptr) { 18 slow = slow -> next; 19 fast = fast -> next -> next; 20 if(slow == fast){ 21 break; 22 } 23 } 24 if (fast == nullptr || fast->next == nullptr) { 25 return nullptr; 26 } 27 slow = head; 28 while (slow != fast) { 29 slow = slow->next; 30 fast = fast->next; 31 } 32 return slow; 33 } 34 };
转载于:https://www.cnblogs.com/wangxiaobao/p/6188596.html
LeetCode141 Linked List Cycle. LeetCode142 Linked List Cycle II相关推荐
- CPU Cycle(CPU 周期)、Instruction Cycle(指令周期)、Clock Cycle(时钟周期)
基本概念 在组成原理以及体系结构中经常遇到 CPU Cycle(CPU 周期).Instruction Cycle(指令周期).Clock Cycle(时钟周期)这些概念,这篇文章详细拆解一下他们之间 ...
- Leetcode142. Linked List Cycle II环形链表2
给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 说明:不允许修改给定的链表. 进阶: 你是否可以不用额外空间解决此题? 方法一:使用map 方法二: 分两个步骤,首先通 ...
- Jan 12 - Delete Node in a Linked List; Data Structure; Linked List; Pointer;
代码: /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* List ...
- LeetCode 142. 环形链表 II(Linked List Cycle II)
142. 环形链表 II 142. Linked List Cycle II 题目描述 给定一个链表,返回链表开始入环的第一个节点.如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整 ...
- [Lintcode]102. Linked List Cycle/[Leetcode]
106. Convert Sorted List to Binary Search Tree/109. Convert Sorted List to Binary Search Tree 本题难度: ...
- 【To Do】LeetCode 142. Linked List Cycle II
LeetCode 142. Linked List Cycle II Solution1:我的答案 这道题多次遇到,牢记此解法 这道题要深思一下,快指针和慢指针的速度对比不同,会产生什么不同的结果? ...
- 数据结构与算法(二)单链表(Singly linked list)
数据结构与算法(二)单链表(Singly linked list) 链表(Linked list) Python完整功能实现 LeetCode思想实践: 链这个东西大家一定都不陌生, 脖子上有项链, ...
- python实现单链表快速排序升序linkedqueue_LeetCode 总结 - 搞定 Linked List 面试题
链表删除 [203] Remove Linked List Elements [19] Remove Nth Node From End of List [83] Remove Duplicates ...
- MegaSAS RAID卡 BBU Learn Cycle周期的影响
背景 最近遇到有些带MegaSAS RAID卡的服务器,在业务高峰时突然IO负载飚升得很高,IO性能急剧下降,查了日志及各种设置最后才发现是RAID卡的Cache写策略由WriteBack变成Writ ...
最新文章
- 服务器打不QQ显示00001,QQ登录超时00001怎么处理?
- [FxCop.设计规则]13. 定义自定义属性参数的访问属性
- 求数组中数对的最大值
- JAVA读取2g数据的速度_Java 读取大容量excel
- Apaceh配置httpd-vhosts虚拟主机
- HDU4578 Transformation(多标记线段树)题解
- FwmarkServer 实现以及功能分析
- Java程序员常用网站
- 智驾仿真-摄像头仿真方案总纲
- 计算机开机错误0xc0000428,启动时出现错误码0xc0000428如何解决?
- 中国农业银行计算机专业笔试题,中国农业银行笔试题库
- 如何成为一名优秀设计师?(锤子科技视觉总监罗子雄演讲实录)
- tdd干扰波形_TDDLTE干扰排查指导书_V1520170219.docx
- 小林:采访Vue作者随想
- 降噪、音质、游戏,实力非凡,泥炭Air3 Pro降噪耳机评测
- 对 Mac 原生终端模拟器的增强
- 一张图掌握薛兆丰经济学讲义的精华
- An attempt was made to call a method that does not exist. The attempt was ma
- voc数据集格式转换为coco数据集格式+修改xml格式文件
- Shell 编程入门(一)
热门文章
- python3编码问题_python3编码问题
- express bodyparser_nodejs库express是如何接收inbound json请求的
- es6 filter方法_ES5和ES6函数你不知道的区别
- 圆周移位是怎么移的_想烟道移位,师傅却连连摆手:小区烟道都是统一的,咋能随便改...
- 计算机网络实验中S1是啥意思,某计算机A需要访问域名www.yy.com,它首先向本域DNS服务器S1查询,.._简答题试题答案...
- EA出品的java射击类游戏,盘点五款射击类RPG游戏:你喜欢这类型游戏吗?
- android mysql sqlite_Android SQLite (一) 数据库简介
- oracle 韩思捷_Oracle数据库技术服务案例精选
- rzsz for linux,linux下如何安装rzsz
- 安卓如何调出软键盘_智能汽车到底如何交互?小鹏用全场景语音给出了答案