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 ...
随机推荐
- python中生成器对象和return 还有循环的区别
python中生成器对象和return 还有循环的区别 在python中存在这么一个关键字yield,这个关键字在项目中经常被用到,比如我写一个函数不想它只返回一次就结束那我们就不能用return,因 ...
- redis 之相关命令
为什么缓存数据库更要首选redis?如何使用redis? 一.使用缓存数据库为什么首选用redis? 我们都知道,把一些热数据存到缓存中可以极大的提高速度,那么问题来了,是用Redis好还是Memca ...
- SSH进阶之路
[SSH进阶之路]Hibernate基本原理(一) 在开始学Hibernate之前,一直就有人说:Hibernate并不难,无非是对JDBC进一步封装.一句不难,难道是真的不难还是眼高手低 ...
- NodeJs初学者经典入门解析
Node.js 是一个基于谷歌浏览器JavaScript执行环境建立的一个平台,让JavaScript可以脱离客户端浏览器运行,让 JavaScript具有服务器语言的能力.我们可以使用NodeJs方 ...
- 【NOIP 2017 普及组】 跳房子
裸的单调队列优化dp+二分 我居然还调了挺久 日常审题错误 #include <bits/stdc++.h> using namespace std; typedef long long ...
- Android 数据存储-文件读写操作
本来已经写了一部分,后来发现这篇博客写的比我的好,就直接引用一下: https://www.cnblogs.com/LiHuiGe8/p/5604725.html
- [python][django学习篇][5]选择数据库版本(默认SQLite3) 与操作数据库
推荐学习博客:http://zmrenwu.com/post/6/ 选择数据库版本(SQLite3) 如果想选择MySQL等版本数据库,请先安装MySQL并且安装python mysql驱动,这里不做 ...
- C++字符串高效查找替换,有空分析分析
void CWebTransfer::Substitute(char *pInput, char *pOutput, char *pSrc, char *pDst) { char *pi, *po, ...
- 一些NGINX配置
一些nginx配置 使用独立目录, 然后include具体配置 gzip on for multi processers static file cache proxy pass 静态目录 or 文件 ...
- preg_replace_callback 正则替换回调方法用法,
Example #1 preg_replace_callback() 和 匿名函数 <?php /* 一个unix样式的命令行过滤器,用于将段落开始部分的大写字母转换为小写. */ $fp = ...