1、题目描述

2、问题分析

使用快慢指针方法判断链表是否有环,然后寻找环开始的节点。

3、代码

 ListNode *detectCycle(ListNode *head) {
if( head == NULL || head->next == NULL ){
return NULL ;
} ListNode* fast = head;
ListNode* slow = head; while( fast != NULL && slow != NULL ){ if( fast->next != NULL ){
fast = fast->next->next;
}else{
return NULL ;
}
slow = slow->next;
if( fast == slow ){
break;
}
} if( fast == NULL || slow == NULL ){
return NULL;
} ListNode* start = head;
while( start != slow ){
start = start->next ;
slow = slow->next;
} return start; }

LeetCode 题解之Linked List Cycle II的更多相关文章

  1. 【LeetCode练习题】Linked List Cycle II

    Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...

  2. LeetCode解题报告—— Linked List Cycle II & Reverse Words in a String & Fraction to Recurring Decimal

    1. Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no ...

  3. 【LeetCode】142. Linked List Cycle II (2 solutions)

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

  4. 【LeetCode】142. Linked List Cycle II

    Difficulty:medium  More:[目录]LeetCode Java实现 Description Given a linked list, return the node where t ...

  5. 【LeetCode】142. Linked List Cycle II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 set 日期 题目地址:https://le ...

  6. LeetCode OJ 142. Linked List Cycle II

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

  7. LeetCode OJ:Linked List Cycle II(循环链表II)

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

  8. 【LeetCode OJ】Linked List Cycle II

    Problem link: http://oj.leetcode.com/problems/linked-list-cycle-ii/ The solution has two step: Detec ...

  9. LeetCode题解之Linked List Cycle

    1.题目描述 2.问题分析 使用快慢指针方法,一个快指针,一个慢指针,如果到某个时候,快指针追上了慢指针,则说明有环存在. 3.代码 bool hasCycle(ListNode *head) { i ...

随机推荐

  1. JavaSE-java8-谓词复合的用法

    谓词接口包括三个方法: negate. and 和 or,让你可以重用已有的Predicate来创建更复杂的谓词 一.比如可以用negate方法来返回一个Predicate非 public class ...

  2. 深入理解SpringCloud之Eureka注册过程分析

    eureka是一种去中心化的服务治理应用,其显著特点是既可以作为服务端又可以作为服务向自己配置的地址进行注册.那么这篇文章就来探讨一下eureka的注册流程. 一.Eureka的服务端 eureka的 ...

  3. OAuth 2.0 安全案例回顾

    原文:http://drops.wooyun.org/papers/598 0x00 背景 纵观账号互通发展史,可以发现OAuth比起其它协议(如OpenID)更流行的原因是,业务双方不仅要求账号本身 ...

  4. WTF小程序之<web-view>

    叨叨两句 昨天爬了一下午坑才出来的我向大家问好

  5. android studio jni调用入门

    一.开发环境配置: 1.Android Studio 2.3.3 2.android-ndk-r14b-windows-x86_64 二.创建项目 1.新建android项目 2.新建文件 3.编译生 ...

  6. redis开机自动服务脚本

    1.设置redis.conf中daemonize为yes,确保守护进程开启. 2.编写开机自启动脚本 基本原理为: 系统开机启动时会去加载/etc/init.d/下面的脚本,通常而言每个脚本文件会自定 ...

  7. Linq 多表连接查询join

    在查询语言中,通常需要使用联接操作.在 LINQ 中,可以通过 join 子句实现联接操作.join 子句可以将来自不同源序列,并且在对象模型中没有直接关系(数据库表之间没有关系)的元素相关联,唯一的 ...

  8. 【转】常见六大Web 安全攻防解析

    原文转自:https://segmentfault.com/a/1190000018073845 作者:浪里行舟 在互联网时代,数据安全与个人隐私受到了前所未有的挑战,各种新奇的攻击技术层出不穷.如何 ...

  9. 使用Akka构建集群(一)

    概述 Akka提供的非常吸引人的特性之一就是轻松构建自定义集群,这也是我要选择Akka的最基本原因之一.如果你不想敲太多代码,也可以通过简单的配置构建一个非常简单的集群.本文为说明Akka集群构建的学 ...

  10. js设计模式总结4

    链模式 链模式:通过在对象方法中将当前对象返回,实现对同一个对象多个方法的链式调用,从而简化多次调用该对象多个方法时的对该对象的多次引用. 具体不多说:主要是方法最后return this; 委托模式 ...