【Leetcode】【Medium】Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
解题:
寻找单链表中环的入口,如果不存在环则返回null。
假设单链表有环,先利用判断单链表是否有环(Linked List Cycle)的方法,找到快慢指针的交点。
假设环的入口在第K个结点处,那么此时快慢指针的交点,距离环入口,还差k个距离。
快慢指针相交后,在链表头位置同时启动另一个指针target,和慢指针once一样每次只移动一步,那么两者同时移动K步,就会相交于入口处。
/**
* 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 || head->next == NULL)
return NULL;
ListNode* once = head;
ListNode* twice = head;
ListNode* target = head; while (twice->next && twice->next->next) {
once = once->next;
twice = twice->next->next;
if (once == twice) {
while (target != once) {
target = target->next;
once = once->next;
}
return target;
}
} return NULL;
}
};
【Leetcode】【Medium】Linked List Cycle II的更多相关文章
- 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现
		
引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...
 - 【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】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题意分析&解答】40. Combination Sum II
		
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
 - 【LeetCode题意分析&解答】37. Sudoku Solver
		
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
 - 【LeetCode题意分析&解答】35. Search Insert Position
		
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
 - 142. Linked List Cycle II【easy】
		
142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If ther ...
 - 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 ...
 - LeetCode解题报告:Linked List Cycle && Linked List Cycle II
		
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
 - LeetCode Linked List Cycle II 和I 通用算法和优化算法
		
Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...
 
随机推荐
- hive Getting Started
			
Apache HiveThe Apache Hive™ data warehouse software facilitates reading, writing, and managing large ...
 - 汇编语言_实验四_[bx]和loop的使用
			
四.实验结论 1. 实验内容1 (1)源代码: assume cs:code code segment mov ax,0b800h mov ds,ax mov bx,07b8h mov c ...
 - Html checkbox全选
			
html中全选 <table class="data-table td-center"> <tr> <td><input type=&qu ...
 - jQuery validate 设置失去焦点就校验和失去焦点就表单校验是否通过
			
js部分 html部分 自定义样式: /*自定义validate覆盖掉了 validate 里面默认的显示样式*/ label.error{ background:url(${pageContext. ...
 - SpringMVC的json交互
			
一.注解说明 1.@RequestBody 作用:@RequestBody注解用于读取http请求的内容(字符串),通过springmvc提供的HttpMessageConverter接口将读到的内 ...
 - 完整maven项目搭建
			
1. 在eclipse中New选择maven项目,并选择web类型这个百度一下到处有: 2. 写简单的controller示例: 3. 配置web.xml,主要是DispatcherServlet: ...
 - 移动端手势事件 hammer.JS插件
			
一.引入hammer.JS 1.下载地址:http://download.csdn.net/detail/webxiaoma/9872249 2.官网地址:http ...
 - android添加系统(服务、应用)
			
1. 添加系统服务 1.1 添加方式1:(不加入servicemanager统一管理的) 看Android6.0.1 init.rc解析中的第2章和第3章 方式1: 1). 写一个测试脚本test.s ...
 - Java中final修饰参数的作用
			
在方法参数前面加final关键字就是为了防止数据在方法体中被修改. 主要分两种情况:第一,用final修饰基本数据类型:第二,用final修饰引用类型. 第一种情况,修饰基本类型(非引用类型).这时参 ...
 - 100个实用的CSS技巧,以及遇见的问题和解决方案。
			
前言 本篇文章将会持续更新,我会将我遇见的一些问题,和我看到的实用的CSS技巧记录下来,本篇文章会以图文混排的方式写下去.具体什么时候写完我也不清楚,反正我的目标是写100个. 本案例都是经本人实测 ...