LeetCode OJ-- Linked List Cycle II **
https://oj.leetcode.com/problems/linked-list-cycle-ii/
判断一个链表中是否有环,如果有,求环的开始位置。
按照上道题目的想法,先判断出是否有环来,同时能得出 slow走了多少步设为 paces。也就是说再次相遇的时候,fast比slow多走了环的n倍的大小。
然后slow = head, fast = head,然后让fast = fast->next往后走 paces 步,之后slow 和 fast再一起走,等到相遇的时候,就是那个环开始地方。
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std; struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
}; class Solution {
public:
ListNode *detectCycle(ListNode *head) {
if(head == NULL)
return false; ListNode *slow = head;
ListNode *fast = head; int space = ;
while(fast)
{
if(fast->next)
fast = fast->next->next;
else
return NULL; slow = slow->next; space++; if(fast == slow)
break;
} if(fast == NULL)
return NULL;
slow = head;
fast = head;
while(space--)
{
fast = fast->next;
}
while(fast!=slow)
{
fast = fast->next;
slow = slow->next;
}
return slow;
}
};
int main()
{
class Solution mys;
ListNode *n1 = new ListNode();
ListNode *n2 = new ListNode();
/*ListNode *n3 = new ListNode(3);
ListNode *n4 = new ListNode(4);*/
n1->next = n2;
/*n2->next = n3;
n3->next = n4;
n4->next = n3;*/
mys.detectCycle(n1);
}
LeetCode OJ-- Linked List Cycle II **的更多相关文章
- [LeetCode OJ] Linked List Cycle II—Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...
- Java for LeetCode 142 Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [Leetcode Week6]Linked List Cycle II
Linked List Cycle II 题解 题目来源:https://leetcode.com/problems/linked-list-cycle-ii/description/ Descrip ...
- 【Leetcode】Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [LeetCode] 142. Linked List Cycle II 链表中的环 II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [LeetCode] 142. Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...
- 【题解】【链表】【Leetcode】Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- (链表 双指针) leetcode 142. Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...
- leetcode 【 Linked List Cycle II 】 python 实现
公司和学校事情比较多,隔了好几天没刷题,今天继续刷起来. 题目: Given a linked list, return the node where the cycle begins. If the ...
- leetcode 142. Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note ...
随机推荐
- Codeforces Round #460 (Div. 2)-D. Substring
D. Substring time limit per test3 seconds memory limit per test256 megabytes Problem Description You ...
- 4 Template层 -模板继承
1.模板继承 模板继承可以减少页面内容的重复定义,实现页面内容的重用 典型应用:网站的头部.尾部是一样的,这些内容可以定义在父模板中,子模板不需要重复定义 block标签:在父模板中预留区域,在子模板 ...
- POJ 3041 Asteroids (二分图最小点覆盖集)
Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 24789 Accepted: 13439 Descr ...
- Redis实现之RDB持久化(一)
RDB持久化 Redis是一个键值对数据库服务器,服务器中通常包含着任意个非空数据库,而每个非空数据库中又可以包含任意个键值对,为了方便起见,我们将服务器中的非空数据库以及它们的键值对统称为数据库状态 ...
- luoguT30204 偷上网
\(n=1\) 时特判四角,其余时刻圆的面积和必小于正方形面积,随机点出来判断就行了. stm 随机算法-- #include <iostream> #include <cstdli ...
- luogu2120 [ZJOI2007]仓库建设
大米饼写的太棒辣qwqqwq #include <iostream> #include <cstdio> using namespace std; typedef long l ...
- IBOutletCollection 索引获取顺序问题
在sb中绑定了一个IBOutletCollection后,根据索引获取元素发现和自己拖线时的顺序不同,有时又会根据顺序,不知道是xcode的bug还是本身就是无序的. 在使用的时候直接排序: - (v ...
- Python对文本文件的简单操作(一)
工作背景 性能测试工程师,主要测试工具--loadrunner,主要是接口测试. 实现功能 loadrunner对报文格式的转换存在问题,部分报文无法转换,故使用Python编写脚本自动将soap协议 ...
- Java 语言概述与开发环境(2)
目录: 一.JDK配置容易出现的问题 二.HelloWorld程序编译常见问题 三.文档注释 四.Java 标识符 五.转义符 六.运算符之算术运算符 ********************** ...
- md5 加密算法和升级
在这里插一小节加密的吧,使用openssl库进行加密. 使用MD5加密 我们以一个字符串为例,新建一个文件filename.txt,在文件内写入hello ,然后在Linux下可以使用命令md5sum ...