链表相关题

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)

分析:

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的距离。
故两指针会在环开始位置相遇。
 
代码:
 /**
* 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的更多相关文章

  1. 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现

    引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...

  2. leetcode142 Linked List Cycle II

    """ Given a linked list, return the node where the cycle begins. If there is no cycle ...

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

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

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

  5. [Linked List]Remove Duplicates from Sorted List II

    Total Accepted: 59433 Total Submissions: 230628 Difficulty: Medium Given a sorted linked list, delet ...

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

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

  8. *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 ...

  9. [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 ...

随机推荐

  1. 计算机基础(day02)

    目录 什么是编程? 计算机的组成 CPU(大脑) 控制器 运算器 存储器 主存 外存 IO设备 input设备(输入设备) output设备(输出设备) 多核cpu 32位和64位 机械硬盘工作原理 ...

  2. BZOJ 1874: [BeiJing2009 WinterCamp]取石子游戏

    Time Limit: 5 Sec Memory Limit: 162 MB Submit: 957 Solved: 394 [Submit][Status][Discuss] Description ...

  3. JavaSE_02_Thread类01

    1.1 并发与并行 并发:指两个或多个事件在同一个时间段内发生. 这在单 CPU 系统中,每一时刻只能有一道程序执行,即微观上这些程序是分时的交替运行,只不过是给人的感觉是同时运行,那是因为分时交替运 ...

  4. 关于CSS3 animation 属性在ie edge浏览器中不能工作

    我想要给div边框加一个闪烁,所以我将css中设置如下 给想要闪烁的div加上blink类  这样在firefox,chrome下是正常显示的,但是在ie下box-shadow属性不能被正常的展现 后 ...

  5. It\'s A Good Day To Die

    [00:01.82]Courage! Duty! Honor! [00:05.67]We call upon our troopers [00:07.90]In this our darkest ho ...

  6. 移动相关的css

    1.首先认识第一个apple-mobile-web-app-capable 删除默认的苹果工具栏和菜单栏. <meta name="apple-mobile-web-app-capab ...

  7. js 获取复选框 和 并改变状态

    function checkAll() { var checkbox = document.getElementById('vegeids');// var boxes = document.getE ...

  8. 在window下远程虚拟机(centos)hadoop运行mapreduce程序

    (注:虽然连接成功但是还是执行不了.以后有时间再解决吧 看到的人别参考仅作个人笔记)先mark下 1.首先在window下载好一个eclipse.和拷贝好linux里面hadoop版本对应的插件(我是 ...

  9. svn: E170013: Unable to connect to a repository at URL svn: E230001: Server SSL certificate verification

    idea更新项目报E230001: Server SSL certificate verification failed: certificate issued for a different hos ...

  10. mongodb集群搭建过程记录

    mongodb集群搭建花费比较长的时间,在此记录下过程,方便以后使用 一 软件环境 系统:ubuntu 18.04,mongodb 社区版4.2 https://docs.mongodb.com/ma ...