linked-list-cycle-ii——链表,找出开始循环节点
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?
快慢指针找重合判断是否循环、而后fast从头开始+1,slow继续前进直到重合
思路解析:
首先我们看一张图;
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
if(head==NULL)
return NULL;
ListNode *fast=head,*slow=head;
ListNode *res=NULL;
while(fast!=NULL&&fast->next!=NULL){
fast=fast->next->next;
slow=slow->next;
if(fast==slow){
res=fast;
break;
}
}
if(res==NULL)
return res; fast=head;
while(fast!=res){
fast=fast->next;
res=res->next;
}
return res;
}
};
linked-list-cycle-ii——链表,找出开始循环节点的更多相关文章
- 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] Linked List Cycle II 链表环起始位置
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [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 ...
- LeetCode之“链表”:Linked List Cycle && Linked List Cycle II
1.Linked List Cycle 题目链接 题目要求: Given a linked list, determine if it has a cycle in it. Follow up: Ca ...
- [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环
题目要求 Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you so ...
- LeetCode 142. 环形链表 II(Linked List Cycle II)
142. 环形链表 II 142. Linked List Cycle II 题目描述 给定一个链表,返回链表开始入环的第一个节点.如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整 ...
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- leetcode 141. Linked List Cycle 、 142. Linked List Cycle II
判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n ...
- [Leetcode Week6]Linked List Cycle II
Linked List Cycle II 题解 题目来源:https://leetcode.com/problems/linked-list-cycle-ii/description/ Descrip ...
随机推荐
- vfs_caches_init函数解析
vfs_caches_init函数初始化VFS,下面梳理函数调用流程 start_kernel() -->vfs_caches_init_early(); -->dcache_init_e ...
- bash快捷键及输入输出重定向
bash特性之快捷键: Ctrl+a: 跳转至命令首部 Ctrl+e: 跳转至命令尾部 Ctrl+l: 清屏 Ctrl+c: 中止或取消 Ctr ...
- CentOS 7.0:搭建SVN服务器
1. 通过 yum install subversion来安装 2. 提示已经安装.查看svn版本 第二步: 创建svn版本库 第三步: 配置svn信息 2. 配置权限配置文件authz 3. 配置用 ...
- 如何完整反编译AndroidMainfest.xml
下载工具: http://code.google.com/p/android4me/downloads/detail?name=AXMLPrinter.zip&can=2&q= 包名为 ...
- xtu数据结构 G. Count the Colors
G. Count the Colors Time Limit: 2000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Jav ...
- android项目引入第三方库工程出现的问题及解决方案
一.导入libar库工程 1.使用第三方库工程libary,基本上都是从github上下载,解压后里面有个libary文件夹 2.将libary导入到eclipse中,步骤如下 1)在eclipse包 ...
- 九度oj 题目1088:剩下的树
题目描述: 有一个长度为整数L(1<=L<=10000)的马路,可以想象成数轴上长度为L的一个线段,起点是坐标原点,在每个整数坐标点有一棵树,即在0,1,2,...,L共L+1个位置上有L ...
- POJ 2643 Election
Election Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3558 Accepted: 1692 Descript ...
- hihoCoder #1471 拥堵的城市
这道题目是hihoCoder Challenge 27的C题,我考虑了5天:(. 计数问题.由于树的结构的特殊性(树具有递归结构),不难想到思路是树形DP.由于这是[计数问题]而非[优化问题],我们思 ...
- BZOJ3124 [Sdoi2013]直径 【树的直径】
题目 小Q最近学习了一些图论知识.根据课本,有如下定义.树:无回路且连通的无向图,每条边都有正整数的权值来表示其长度.如果一棵树有N个节点,可以证明其有且仅有N-1 条边. 路径:一棵树上,任意两个节 ...