判断是不是循环链表,如果是,返回它的第一个结点

首先判断,判断完之后,遍历循环链表,将它的指针域置为空,则循环到链表的第一个结点时,由于指针域为空,返回

#include<iostream>
#include<vector>
using namespace std;
/*
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.
Follow up:
Can you solve it without using extra space?*/
struct ListNode {int val;ListNode *next;ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:bool hasCycle(ListNode *head) {if (head == NULL)return false;ListNode* faster = head;ListNode* slower = head;while (faster->next->next != NULL && faster->next != NULL) {slower = slower->next;faster = faster->next->next;if (faster == slower)return true;}return false;}
};
//class Solution {  //没有通过所有测试用例
//public:
//  ListNode *detectCycle(ListNode *head) {
//      if (head == NULL)
//          return NULL;
//      ListNode* faster = head;
//      ListNode* slower = head;
//      while (faster->next->next != NULL && faster->next != NULL) {
//          slower = slower->next;
//          faster = faster->next->next;
//          if (faster == slower)   //当他两第一轮相同时,正好是最后一个结点
//              return slower;
//      }
//      return NULL;
//  }
//};
class Solution {
public:bool hasCycle(ListNode *head) {ListNode *fast = head;ListNode *slow = head;while (fast != NULL && fast->next != NULL) {fast = fast->next->next;slow = slow->next;if (fast == slow)return true;}return false;}ListNode *detectCycle(ListNode *head) {if (hasCycle(head)) {ListNode *temp = head;while (head->next) {temp = head->next;head->next = NULL;head = temp;}return head;}else {return NULL;}}
};

Leedcode10-linked-list-cycle-ii相关推荐

  1. leetcode day5 -- Reorder List Linked List Cycle II

    1.  Reorder List Given a singly linked list L: L0→L1→-→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln ...

  2. LeetCode141 Linked List Cycle. LeetCode142 Linked List Cycle II

    链表相关题 141. Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can ...

  3. Leetcode 142. Linked List Cycle II

    地址:Leetcode 142. linked list Cycle II 问题描述:检测链表是否存在环,是的话返回环入口,否则返回None. 这道题有两个思路,一个是经典的快慢指针的思路,另外一个是 ...

  4. LeetCode 142. 环形链表 II(Linked List Cycle II)

    142. 环形链表 II 142. Linked List Cycle II 题目描述 给定一个链表,返回链表开始入环的第一个节点.如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整 ...

  5. 【To Do】LeetCode 142. Linked List Cycle II

    LeetCode 142. Linked List Cycle II Solution1:我的答案 这道题多次遇到,牢记此解法 这道题要深思一下,快指针和慢指针的速度对比不同,会产生什么不同的结果? ...

  6. Linked List Cycle II

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

  7. leetcode: Linked List Cycle II

    http://oj.leetcode.com/problems/linked-list-cycle-ii/ Given a linked list, return the node where the ...

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

  9. 【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 ...

  10. LeetCode Linked List Cycle II(floyd cycle)

    问题:给出一个链表,可能存在环,要求输出环的起点位置 思路:使用Floyd cycle algorithm 具体代码参考: https://github.com/wuli2496/OJ/tree/ma ...

最新文章

  1. linux查看机器所有寄存器,linux系统下操作硬件寄存器
  2. 金蝶清空日志数据库脚本
  3. getplotlyoffline(‘http://cdn.plot.ly/plotly-latest.min.js‘)无法下载如何解决
  4. linux centos版本安装软件,linux – 在CentOS上安装软件:二进制文件还是rpm?
  5. PyTorch深度学习实践07
  6. Nutshell中的Java 8语言功能-第1部分
  7. Java分布式篇5——FastDFS
  8. 语言中的petchar运用_自闭症儿童语言障碍家庭训练,需要融入这些方法
  9. 禹洲:我们这一代人的困惑
  10. Spring : @EnableScheduling注解 @Scheduled
  11. 常用位操作以及相关原理
  12. utuntu 视频 无声
  13. npp 插件html,Notepad++中常用的插件
  14. R语言绘图(一)热图
  15. flutter listview 设置分割线
  16. 微信会员卡-创建会员卡接口post参数字段说明
  17. 【洛谷P2000】拯救世界
  18. scrapy写爬虫的心得
  19. c语言 准确赋值浮点数,C语言浮点数
  20. php GD库的使用

热门文章

  1. Java核心类库篇8——网络编程
  2. Java核心类库篇2——lang
  3. mysql 查询商品列表 显示tag_让前台页面商品列表显示后台数据库中的商品
  4. python中__init__方法_关于python中__init__方法理解
  5. 计算机专业合成词,大学计算机论文范文大全.docx
  6. java 观察者模式_重学 Java 设计模式:实战观察者模式「模拟类似小客车指标摇号过程,监听消息通知用户中签场景」...
  7. openssh升级后root_又一root神器停止营业!时至今日你还需要root吗
  8. Spring Boot————BeanCreationNotAllowedException异常分析
  9. mlag 堆叠_S-MLAG解决方案介绍
  10. mysql 客户服务号_mysql客户端及服务端常用实用工具功能总结