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还没有相遇说明没有环。
代码:
/**
* 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) {
if (head == NULL) {
return ;
}
ListNode* slow = head;
ListNode* fast = head;
while (fast != nullptr && fast->next != nullptr) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast) {
return true;
}
}
return false;
}
};
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)
分析:
/**
* 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 == nullptr) {
return ;
}
ListNode* slow = head;
ListNode* fast = head;
while (fast != nullptr && fast->next != nullptr) {
slow = slow -> next;
fast = fast -> next -> next;
if(slow == fast){
break;
}
}
if (fast == nullptr || fast->next == nullptr) {
return nullptr;
}
slow = head;
while (slow != fast) {
slow = slow->next;
fast = fast->next;
}
return slow;
}
};
LeetCode141 Linked List Cycle. LeetCode142 Linked List Cycle II的更多相关文章
- 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现
引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...
- leetcode142 Linked List Cycle II
""" Given a linked list, return the node where the cycle begins. If there is no cycle ...
- Leetcode142. Linked List Cycle II环形链表2
给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 说明:不允许修改给定的链表. 进阶: 你是否可以不用额外空间解决此题? 方法一:使用map 方法二: 分两个步骤,首先通 ...
- 141. Linked List Cycle&142. Linked List Cycle II(剑指Offer-链表中环的入口节点)
题目: 141.Given a linked list, determine if it has a cycle in it. 142.Given a linked list, return the ...
- [Linked List]Remove Duplicates from Sorted List II
Total Accepted: 59433 Total Submissions: 230628 Difficulty: Medium Given a sorted linked list, delet ...
- LeetCode[Linked List]: Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [LeetCode] 203. Remove Linked List Elements_Easy tag: Linked LIst
Remove all elements from a linked list of integers that have value val. Example: Input: 1->2-> ...
- *Amazon problem: 234. Palindrome Linked List (reverse the linked list with n time)
Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...
- [LeetCode] 92. Reverse Linked List II_Medium tag: Linked List
Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...
随机推荐
- JavaSE_08_Collections常用功能
1.1 常用功能 java.utils.Collections是集合工具类,用来对集合进行操作.部分方法如下: public static <T> boolean addAll(Colle ...
- Joomla - akeeba backup(joomla网站备份、迁移扩展)
在所有 joomla 的网站中,如果只允许安装一个扩展,估计超过90%的人都会选择 akeeba backup,这基本是每个joomla都必备的一个扩展: akeeba backup 的更多资料可以到 ...
- 19-10-23-K-Aft
没改完题就过来沽博客是不是有点不好…… ZJ一下: 好好好题. T1数组大小…… $$10^7 \rightarrow 60$$ 事实上…… $$7 \times 10^7 \rightarrow 0 ...
- 入门servlet:request请求转发和共享数据
request 请求转发:一种在服务器内部的资源跳转方式 步骤: 1.通过request对象获取请求转发器对象:RequestDispatcher getRequestDispatcher(Strin ...
- Promise的源码实现(符合Promise/A+规范)
我们手写一个Promise/A+规范,然后安装测试脚本,以求通过这个规范. //Promise/A+源代码 // new Promise时,需要传递一个executor执行器,执行器立即执行 // e ...
- Liferay 7:Liferay DXP解决方案
分享是美德,欢迎探讨技术 这个作者很厉害呀,写的博客都是解决很刁钻问题的.强烈推荐 http://www.liferaysolution.com/2017/06/captcha-recaptcha-w ...
- MAC中已有的虚拟环境在pycharm 中进行调用
首先确定虚拟环境的路径: 打开工作环境配置文件,找到工作目录: 在pycharm中解释器中找到工作目录中对应的虚拟环境进行配置就可以了
- 在VUE中实现打印
1.安装 npm install vue-print-nb --save 2.在mian.js中引入 import Print from 'vue-print-nb' Vue.use(Print); ...
- [转]深入理解定位父级offsetParent及偏移大小
偏移量(offset dimension)是javascript中的一个重要的概念.涉及到偏移量的主要是offsetLeft.offsetTop.offsetHeight.offsetWidth这四个 ...
- 建筑设计类软件整理ACDSee,PS,CAD,Ecotect,SketchUp,Phoenics,Revit,Rhino,
关于建筑设计类软件整理 1 建筑设计类专业最常用软件如下,包括SU,CAD, REVIT,PS等. 2 sketch up 简称SU,中文名草图大师,在推敲方案,拉推体块时有着不可或缺的作用,而且随着 ...