链表相关题

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)同linked-list-cycle-i一题,使用快慢指针方法,判定是否存在环,并记录两指针相遇位置(Z);
2)将两指针分别放在链表头(X)和相遇位置(Z),并改为相同速度推进,则两指针在环开始位置相遇(Y)。
证明如下:
如下图所示,X,Y,Z分别为链表起始位置,环开始位置和两指针相遇位置,则根据快指针速度为慢指针速度的两倍,可以得出:
2*(a + b) = a + b + n * (b + c);即
a=(n - 1) * b + n * c = (n - 1)(b + c) +c;
注意到b+c恰好为环的长度,故可以推出,如将此时两指针分别放在起始位置和相遇位置,并以相同速度前进,当一个指针走完距离a时,另一个指针恰好走出 绕环n-1圈加上c的距离。
故两指针会在环开始位置相遇。
代码:
 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相关推荐

  1. CPU Cycle(CPU 周期)、Instruction Cycle(指令周期)、Clock Cycle(时钟周期)

    基本概念 在组成原理以及体系结构中经常遇到 CPU Cycle(CPU 周期).Instruction Cycle(指令周期).Clock Cycle(时钟周期)这些概念,这篇文章详细拆解一下他们之间 ...

  2. Leetcode142. Linked List Cycle II环形链表2

    给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 说明:不允许修改给定的链表. 进阶: 你是否可以不用额外空间解决此题? 方法一:使用map 方法二: 分两个步骤,首先通 ...

  3. 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 ...

  4. LeetCode 142. 环形链表 II(Linked List Cycle II)

    142. 环形链表 II 142. Linked List Cycle II 题目描述 给定一个链表,返回链表开始入环的第一个节点.如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整 ...

  5. [Lintcode]102. Linked List Cycle/[Leetcode]

    106. Convert Sorted List to Binary Search Tree/109. Convert Sorted List to Binary Search Tree 本题难度: ...

  6. 【To Do】LeetCode 142. Linked List Cycle II

    LeetCode 142. Linked List Cycle II Solution1:我的答案 这道题多次遇到,牢记此解法 这道题要深思一下,快指针和慢指针的速度对比不同,会产生什么不同的结果? ...

  7. 数据结构与算法(二)单链表(Singly linked list)

    数据结构与算法(二)单链表(Singly linked list) 链表(Linked list) Python完整功能实现 LeetCode思想实践: 链这个东西大家一定都不陌生, 脖子上有项链, ...

  8. python实现单链表快速排序升序linkedqueue_LeetCode 总结 - 搞定 Linked List 面试题

    链表删除 [203] Remove Linked List Elements [19] Remove Nth Node From End of List [83] Remove Duplicates ...

  9. MegaSAS RAID卡 BBU Learn Cycle周期的影响

    背景 最近遇到有些带MegaSAS RAID卡的服务器,在业务高峰时突然IO负载飚升得很高,IO性能急剧下降,查了日志及各种设置最后才发现是RAID卡的Cache写策略由WriteBack变成Writ ...

最新文章

  1. 服务器打不QQ显示00001,QQ登录超时00001怎么处理?
  2. [FxCop.设计规则]13. 定义自定义属性参数的访问属性
  3. 求数组中数对的最大值
  4. JAVA读取2g数据的速度_Java 读取大容量excel
  5. Apaceh配置httpd-vhosts虚拟主机
  6. HDU4578 Transformation(多标记线段树)题解
  7. FwmarkServer 实现以及功能分析
  8. Java程序员常用网站
  9. 智驾仿真-摄像头仿真方案总纲
  10. 计算机开机错误0xc0000428,启动时出现错误码0xc0000428如何解决?
  11. 中国农业银行计算机专业笔试题,中国农业银行笔试题库
  12. 如何成为一名优秀设计师?(锤子科技视觉总监罗子雄演讲实录)
  13. tdd干扰波形_TDDLTE干扰排查指导书_V1520170219.docx
  14. 小林:采访Vue作者随想
  15. 降噪、音质、游戏,实力非凡,泥炭Air3 Pro降噪耳机评测
  16. 对 Mac 原生终端模拟器的增强
  17. 一张图掌握薛兆丰经济学讲义的精华
  18. An attempt was made to call a method that does not exist. The attempt was ma
  19. voc数据集格式转换为coco数据集格式+修改xml格式文件
  20. Shell 编程入门(一)

热门文章

  1. python3编码问题_python3编码问题
  2. express bodyparser_nodejs库express是如何接收inbound json请求的
  3. es6 filter方法_ES5和ES6函数你不知道的区别
  4. 圆周移位是怎么移的_想烟道移位,师傅却连连摆手:小区烟道都是统一的,咋能随便改...
  5. 计算机网络实验中S1是啥意思,某计算机A需要访问域名www.yy.com,它首先向本域DNS服务器S1查询,.._简答题试题答案...
  6. EA出品的java射击类游戏,盘点五款射击类RPG游戏:你喜欢这类型游戏吗?
  7. android mysql sqlite_Android SQLite (一) 数据库简介
  8. oracle 韩思捷_Oracle数据库技术服务案例精选
  9. rzsz for linux,linux下如何安装rzsz
  10. 安卓如何调出软键盘_智能汽车到底如何交互?小鹏用全场景语音给出了答案