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. Redis之set类型操作

    接口: package com.net.test.redis.base.dao; /** * @author*** * @Time:2017年8月10日 下午2:32:12 * @version 1. ...

  2. 常用模块之 os,json,shelve,xml模块

    os 即操作系统 在 os 中提供了很多关于文件,文件夹,路径处理的函数 这是我们学习的重点 os.path 是os模块下专门用于处理路径相关的 python是一门跨平台语言,由于每个平台路径规则不同 ...

  3. csapp 深入理解计算机系统 csapp.h csapp.c文件配置

    转载自   http://condor.depaul.edu/glancast/374class/docs/csapp_compile_guide.html Compiling with the CS ...

  4. Zookeeper CreateMode

    通过CreateMode 可以设置在zookeeper中创建节点的类型,节点类型共有4种: EPHEMERAL:临时节点 EPHEMERAL_SEQUENTIAL:有序的临时节点 PERSISTENT ...

  5. 频繁模式挖掘中Apriori、FP-Growth和Eclat算法的实现和对比(Python实现)

    最近上数据挖掘的课程,其中学习到了频繁模式挖掘这一章,这章介绍了三种算法,Apriori.FP-Growth和Eclat算法:由于对于不同的数据来说,这三种算法的表现不同,所以我们本次就对这三种算法在 ...

  6. 【Luogu】P2488工作安排(费用流)

    题目链接 这题……费用流即可……(哇啊要被打死辣) 然而我printf("%d")爆零四次 好的心如死灰 #include<cstdio> #include<cs ...

  7. EasyUI序列化提交学习总结

    jquery easyui将form表单元素的值序列化成对象 form表单 <form id="ff"> <input type="text" ...

  8. docke存储

    1.Docker提供三种不同的方式将数据从宿主机挂载到容器中:volumes,bind mounts和tmpfs.volumes:Docker管理宿主机文件系统的一部分(/var/lib/docker ...

  9. 从零开始实现放置游戏(六)——实现挂机战斗(4)导入Excel数值配置

    前面我们已经实现了在后台管理系统中,对配置数据的增删查改.但每次添加只能添加一条数据,实际生产中,大量数据通过手工一条一条添加不太现实.本章我们就实现通过Excel导入配置数据的功能.这里我们还是以地 ...

  10. vue2搭建简易spa

    使用vue-cli来配置webpack,webpack是一个打包工具,使程序模块化 全局安装vue-cli: npm install -g vue-cli 安装好后,使用vue-cli脚手架配置web ...