剑指Offer(书):链表中环的入口节点
题目:给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。
public ListNode EntryNodeOfLoop(ListNode pHead) {
//第一步,查找是否有环。
ListNode meetingNode = findIsHoop(pHead);
if (meetingNode == null) {
return null;
}
//第二步,查找环中节点的个数
int number = findNodeNumberForHoop(meetingNode);
//第三步,查找入口节点
ListNode tempHead = pHead;
for (int i = 0; i < number; i++) {
tempHead = tempHead.next;
}
ListNode tempHead1 = pHead;
while (tempHead != tempHead1) {
tempHead = tempHead.next;
tempHead1 = tempHead1.next;
}
return tempHead;
}
/**
* 查找环中节点的数量
* @param meetingNode 环中的节点
* @return 节点的数量
*/
private int findNodeNumberForHoop(ListNode meetingNode) {
ListNode tempNode = meetingNode;
int number = 1;
while (meetingNode.next != tempNode) {
meetingNode = meetingNode.next;
number++;
}
return number;
}
/**
* 寻找是否有环。使用两个指针来判断,一个指针走一步,一个指针走两步,若有环则走两步的必定能够追上走一步的。
*
* @param pHead 头结点
* @return 若有环,返回环中的某一位置;否则,返回null;
*/
private ListNode findIsHoop(ListNode pHead) {
if (pHead == null) {
return null;
}
ListNode pSlow = pHead.next;
if (pSlow == null) {
return null;
}
ListNode pFast = pSlow.next;
if (pFast == null) {
return null;
}
while (pFast != null && pSlow != null) {
if (pFast == pSlow) {
return pFast;
}
pSlow = pSlow.next;
pFast = pFast.next;
if (pFast != null) {
pFast = pFast.next;
}
}
return null;
}
剑指Offer(书):链表中环的入口节点的更多相关文章
- 剑指Offer:链表中环的入口节点【23】
剑指Offer:链表中环的入口节点[23] 题目描述 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null. 题目分析 第一步确定链表中是否包含环,怎么确定呢?我们定义两个指针橙和 ...
- 剑指offer——25链表中环的入口节点
题目描述 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null. 题解: 使用快慢指针即可,若快慢指针会相遇,则有环,否则快指针先到空节点: 此时,快指针从此处一次移一步遍历, ...
- 【剑指Offer】链表中环的入口结点 解题报告(Python)
[剑指Offer]链表中环的入口结点 解题报告(Python) 标签(空格分隔): 剑指Offer 题目地址:https://www.nowcoder.com/ta/coding-interviews ...
- [剑指Offer]23-链表中环的入口节点
题目链接 https://www.nowcoder.com/practice/253d2c59ec3e4bc68da16833f79a38e4?tpId=13&tqId=11208&t ...
- 【Java】 剑指offer(23) 链表中环的入口结点
本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 一个链表中包含环,如何找出环的入口结点?例如,在图3.8的链表中, ...
- Go语言实现:【剑指offer】链表中环的入口结点
该题目来源于牛客网<剑指offer>专题. 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null. Go语言实现: /** * Definition for sing ...
- 剑指offer:链表中环的入口结点
题目描述: 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null. 思路分析: 这道题首先需要判断链表是否存在环,很快就能想到用快慢指针来判断. 由于快慢指针的相遇位置并不一定为链 ...
- 剑指Offer 55. 链表中环的入口结点 (链表)
题目描述 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null. 题目地址 https://www.nowcoder.com/practice/253d2c59ec3e4bc68d ...
- [剑指Offer] 55.链表中环的入口结点
题目描述 一个链表中包含环,请找出该链表的环的入口结点. [思路]根据set集合的不重复,遍历链表时遇到的第一个重复结点就是环的入口结点. /* struct ListNode { int val; ...
- 《剑指offer》-链表找环入口
题目描述 一个链表中包含环,请找出该链表的环的入口结点. 初步想法是每个节点做几个标记,表示是否被访问过,那么遍历链表的时候就知道哪个被访问到了.但是不会实现. 另一个直觉是判断链表有环的算法中出现过 ...
随机推荐
- C# 文件操作 全收录 追加、拷贝、删除、移动文件、创建目录、递归删除文件夹及文件....
本文收集了目前最为常用的C#经典操作文件的方法,具体内容如下:C#追加.拷贝.删除.移动文件.创建目录.递归删除文件夹及文件.指定文件夹下 面的所有内容copy到目标文件夹下面.指定文件夹下面的所有内 ...
- 最耗资源的10条sql
----当前最耗资源的10个cpu select * from (select address,hash_value, round(cpu_time/1000000) cpu_time_s, roun ...
- Unity 360 旋转 缩放
using UnityEngine; using System.Collections; public class SandR : MonoBehaviour { public GameObject ...
- 【转】onAttachedToWindow()在整个Activity生命周期的位置及使用
上篇博客实现圆角对话框样式的Activity中提到,若需实现圆角对话框Activity,需要在Activity的onAttachedToWindow()函数中做文章,那么就想问: onAttached ...
- linux服务器上的jenkins远程触发构建windows server 2012服务器上的jenkins任务
本文来自:https://blog.csdn.net/huashao0602/article/details/53318295 非常感谢原博主,亲测可行,这是我做CI持续集成试过的第6套方案了! 背 ...
- 如何修改tomcat的启动方式为 run
tomcat根目录\bin\startup.bat,记事本打开,找到: call "%EXECUTABLE%" start %CMD_LINE_ARGS% :end 把start ...
- vue+element ui项目总结点(三)富文本编辑器 vue-wangeditor
1.参考 https://www.npmjs.com/package/vue-wangeditor 使用该富文本编辑器 <template> <div class="egi ...
- vba 时间
Sub tt1() Dim d1, d2 As Date d1 = #//# d2 = #//# Debug.Print "相隔" & (d2 - d1) & &q ...
- The Singapore NRIC Check Digit
The Singapore NRIC number is made up of 7 digits and a letter behind. This letter is calculated from ...
- Python学习日志9月13日
昨天的学习日志没有写,乱忙了一整天,政治电脑. 好奇心重,想要给电脑装上传说中LInux操作系统,各种小问题折腾到半夜,今天又折腾到晚上才真正的装上系统. 可是装上系统后又发现各种的不好用.虽然界面比 ...