【leetcode】Linked List Cycle II (middle)
Given a linked list, return the node where the cycle begins. If there is no cycle, return null
.
Follow up:
Can you solve it without using extra space?
思路:
做过,就当复习了。
先用快慢指针判断相交,关键是环开始点的获取。
用上图说明一下,设非环的部分长度为a(包括环的入口点), 环的长度为b(包括环的入口点). 快慢指针相交的位置为绿色的点,距离环的开始位置为c(以环的入口点距离为0算)。
因为快指针的速度是慢指针的两倍。 故: a + f*b + c = 2(a + s*b + c)
得出: a + c = k * b
即此时一个指针从相交点开始走,距离入口为c, 再走一个a的距离 就会到达入口点,也就是说从相交点开始走的指针会和从头结点开始走的指针在入口点相交。 返回这个交点就可以了。
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std; // 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) {
ListNode * fast = head;
ListNode * slow = head;
ListNode * p = NULL;
//用快慢指针判断是否有环 快指针一次走两步 慢指针一次一步 有环会相遇
while(fast != NULL)
{
fast = (fast->next == NULL) ? NULL : fast->next->next;
slow = slow->next;
if(slow != NULL && slow == fast)
{
p = slow; //记录相遇的位置
break;
}
}
if(p == NULL) //如果没有相遇 没有环
{
return NULL;
}
else
{
ListNode * cross = head;
while(cross != p) //新的结点从头开始走 另一节点从之前相遇的位置开始走 这两个结点相遇的位置就是环的入口
{
cross = cross->next;
p = p->next;
}
return cross;
}
} };
【leetcode】Linked List Cycle II (middle)的更多相关文章
- 【leetcode】Path Sum I & II(middle)
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- 【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】Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- 【LeetCode】Linked List Cycle II(环形链表 II)
这是LeetCode里的第142道题. 题目要求: 给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 说明:不允许修改给定的链表. 进阶:你是否可以不用额外空间解决此题? ...
- 【leetcode】Swap Nodes in Pairs (middle)
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- 【leetcode】Search for a Range(middle)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- 【leetcode】Binary Search Tree Iterator(middle)
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- 【leetcode】Validate Binary Search Tree(middle)
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- 【leetcode】Minimum Size Subarray Sum(middle)
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
随机推荐
- Mastering Web Application Development with AngularJS 读书笔记(一)
第一章笔记 (一) 一.PS:运行时配置IIS <html> <head> <script src="angular.js"></scri ...
- AngularJS 使用$sce控制代码安全检查
由于浏览器都有同源加载策略,不能加载不同域下的文件.也不能使用不合要求的协议比如file进行访问. 在angularJs中为了避免安全漏洞,一些ng-src或者ng-include都会进行安全校验,因 ...
- 网站SEO优化之添加Sitemap文件。
Sitemap.xml 故名思意就是站点地图文件,可以指引Google spider 收录相应网页.正确地使用Google Sitemap,可以确保让Google spider 不遗漏网站内的任何页面 ...
- TP数据访问
重点学习了: 1,ThinkPHP查询数据 2.ThinkPHP添加数据 LianXiController.class.php <?php namespace Home\Controller; ...
- Hadoop之Storm命令
Hadoop之Storm命令 1.storm核心概念 stream--->一列火车 tuple--->一节车厢 数据--->乘客 spout--->始发站 bolt---> ...
- redis.1--SDS结构
1. Redis 没有直接使用c语言的字符串(以空字符结尾的字符数组),而是自己构建了一 种名为简单动态字符串(Simple Dynamic String , SDS),并将SDS做为 ...
- plsql 建立oracle作业
--.plsql中学习job --学习job --建表 create table test_job(para_date date); commit; insert into test_job valu ...
- webpack 教程 那些事儿06-gulp+webpack多页
本篇主要讲述用gulp+webpack构建多页应用 折腾到现在,项目还必须要进行,.vue文件必须要加载,也就是webpack必须引入.时间不多了,抛弃上个方案之后,只能牺牲热加载性能,用gulp+w ...
- 雪峰配置的nginx
- 水面shader 线性擦除
// Upgrade NOTE: replaced 'PositionFog()' with multiply of UNITY_MATRIX_MVP by position // Upgrade N ...