LeetCode 题解之Linked List Cycle II
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的更多相关文章
- 【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 ...
- 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 ...
- 【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 ...
- 【LeetCode】142. Linked List Cycle II
Difficulty:medium More:[目录]LeetCode Java实现 Description Given a linked list, return the node where t ...
- 【LeetCode】142. Linked List Cycle II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 set 日期 题目地址:https://le ...
- 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 ...
- 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 ...
- 【LeetCode OJ】Linked List Cycle II
Problem link: http://oj.leetcode.com/problems/linked-list-cycle-ii/ The solution has two step: Detec ...
- LeetCode题解之Linked List Cycle
1.题目描述 2.问题分析 使用快慢指针方法,一个快指针,一个慢指针,如果到某个时候,快指针追上了慢指针,则说明有环存在. 3.代码 bool hasCycle(ListNode *head) { i ...
随机推荐
- Objective-C 字符串与数值互相转换
Convert NSString to int NSString *aNumberString = @"123"; int i = [aNumberString intValue] ...
- 分步理解 Promise 的实现
一个 Promise 的运用: var firstPromise = new Promise(function(resolve,reject){ setTimeout(function(){ var ...
- linux centos挂载数据盘教程
一.备份/home/liying目录数据前提条件:电脑重启下,保证服务关闭,以免进程影响操作 a.新建backup目录#cd /#mkdir backup b.把/home/liying/目录下的数据 ...
- Tomcat8源码编译及导入Eclipse中研究
最近因为需求需要修改Tomcat中的某些功能,无奈只能研究那部分源码然后稍作修改. 目前Tomcat最新版是8.0,下载了源码包,编译并导入Eclipse中研究比较方便. 1. Tomcat8源码编译 ...
- 面试题-----按位翻转32位unsigned
// test.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include < ...
- Linux后台运行java的jar包
Linux 运行jar包命令如下: 方式一 特点:当前ssh窗口被锁定,可按CTRL + C打断程序运行,或直接关闭窗口,程序退出 那如何让窗口不锁定? 方式二 java -jar shareniu. ...
- Python面向对象基础一
公司可能过一两个月就要从深圳搬到东莞松山湖,项目组的现在有的在转Java或其他语言的,问我们要不要转java+hoodap+spark方向,我还是先不转,毕竟之前是从ios转回C#,这现在在转其他的那 ...
- 【转载】图片 CSS:怎样才能 “响应式 + 固定宽高比例”?
自己根据项目需要,把代码摘了出来 <div class="img-box"></div> .img-box{ height:0; padding-botto ...
- 移动端常见bug汇总001
点击样式闪动 Q: 当你点击一个链接或者通过Javascript定义的可点击元素的时候,它就会出现一个半透明的灰色背景. A:根本原因是-webkit-tap-highlight-color,这个属性 ...
- EWS 通过SubscribeToPullNotifications订阅Exchange删除邮件
摘要 在使用拉通知的方式监听exchange邮件的时候,无法监听到收件箱删除的邮件.最后通过调试发现,在删除收件箱邮件的时候,是将收件箱的邮件移动到了deleted item文件夹,会触发Moved事 ...