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 **的更多相关文章

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

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

  3. [Leetcode Week6]Linked List Cycle II

    Linked List Cycle II 题解 题目来源:https://leetcode.com/problems/linked-list-cycle-ii/description/ Descrip ...

  4. 【Leetcode】Linked List Cycle II

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

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

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

  7. 【题解】【链表】【Leetcode】Linked List Cycle II

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

  8. (链表 双指针) 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 ...

  9. leetcode 【 Linked List Cycle II 】 python 实现

    公司和学校事情比较多,隔了好几天没刷题,今天继续刷起来. 题目: Given a linked list, return the node where the cycle begins. If the ...

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

随机推荐

  1. list,tuple,set,dict汇总

      有序/无序 追加/删除元素 元素可/不可重复 元素类型 创建方式 List 有序 可追加删除追加:list.append(item),list.insert(index,item)删除:list. ...

  2. Understanding on 'Error to Origin (50x)' , 'Internal CDN Error (50x)' and 'External Error (50x)' in Chartron

    Overview This document explains about definition of these values on OUI Chartron. Definition of Erro ...

  3. unknow table alarmtemp error when drop database (mysql)

    Q: unknow table alarmtemp error when  drop database (mysql) D: alarmtemp is table in rtmd database. ...

  4. centos使用--zsh

    目录 1 切换到zsh 1.1 查看系统当前的shell 1.2 查看bin下是否有zsh包 1.3 安装zsh包 1.4 切换shell至zsh 2 安装oh-my-zsh 2.1 oh-my-zs ...

  5. 非常全面的vim配置文件

    1.mac下vim全局配置目录 /usr/share/vim/vimrc 一般不对此文件做修改,在用户目录下创建自定义配置,目录为: /Users/xxxxx cd ~ 2自定义vim配置 配置功能: ...

  6. [python][django学习篇][11]后台admin用户登录博客,添加文章---这一章和博客首页设计没有关系

    1 如果没有创建超级管理员账号,先要创建python manage.py createsuperuser 2 在admin后台注册模型(如果没有这一步,登录http://127.0.0.1:8000/ ...

  7. [问题解决]docker启动不了

    问题描述:昨天下午整合了同事的代码,发现docker启动好后,docker ps查看不到,docker ps -a发现docker容器没有启动. 尝试多次启动发现都是启动不了. 经过搜索发现 http ...

  8. Eureka 简介以及简单示例(创建EurekaServer工程)

    Eureka 是一款开源的服务注册与发现组件,通过配合其他组件可提供负载均衡能力. 服务发现类型的技术对比: 名称 类型 AP/CP 语言 依赖 集成 一致性算法 Eureka General AP ...

  9. 在线人数统计session管理

    下午比较闲(其实今天都很闲),想了一下在线人数统计方面的实现,上网找了下这方面的知识,最初我的想法是,管理session,如果session销毁了就减少,如果登陆用户了就新增一个,但是如果是用户非法退 ...

  10. OOP的三大特性------封装、继承、多态

    封装 1.<1>类背后隐藏的思想是数据抽象和封装 <2>信息隐藏,隐藏对象的实现细节,不让外部直接访问到 将数据成员和成员函数一起包装到一个单元里,单元以类的形式实现 < ...