单链表是否有环的问题解决与讨论(java实现)
单链表是否有环的问题经常在面试中遇到,一般面试中会要求空间为O(1);再者求若有环,则求环产生时的起始位置。
下面采用java实现。
//单链表
class ListNode{
int val;
ListNode next;
ListNode(int x){
val=x;
next=null;
}
}
public class SearchCycleNode{
ListNode equalListNode=null;//来记录判断有环时出现的相等的时的节点,姑且叫"相遇"节点。
//从"相遇"节点出发,第一个可达的节点(从单链表的头节点开始)即是单链表的环产生的起始位置
public ListNode detectCycle(ListNode head) {
if(!hasCycle(head)) return null;
ListNode p=head;
while(!isReach(p)){
p=p.next;
}
return p; }
//判断从相遇的节点到 head节点可达性
private boolean isReach(ListNode head){
if(head==equalListNode)return true;
ListNode p=equalListNode.next;
while(p!=equalListNode){
if(p==head)return true;
p=p.next;
}
return false;
}
//判断是否有环,通过一个指针p走一步,一个指针q走两步,如果能出现p=q的情况,则有环,并记录p为"相遇"节点。
private boolean hasCycle(ListNode head) {
if(head==null)return false;
if(head.next==null)return false;
ListNode p=head;
ListNode q=head.next;
while(p!=q){
if(p.next==null)return false;
p=p.next;
if(q.next==null)return false;
if(q.next.next==null)return false;
q=q.next.next;
}
equalListNode=p;
return true; }
}
单链表是否有环的问题解决与讨论(java实现)的更多相关文章
- [LeetCode] Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [CareerCup] 2.6 Linked List Cycle 单链表中的环
2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...
- 如何判断单链表是否存在环 & 判断两链表是否相交
给定一个单链表,只给出头指针h: 1.如何判断是否存在环? 2.如何知道环的长度? 3.如何找出环的连接点在哪里? 4.带环链表的长度是多少? 解法: 1.对于问题1,使用追赶的方法,设定两个指针sl ...
- [LeetCode] 142. Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...
- [算法][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 ...
- C# 判断一个单链表是否有环及环长和环的入口点
1.为什么写这个随笔? 前几天参加一个电面,被问到这个问题,想总结一下. 2.为什么标题强调C#? 想在网上看看代码,却没找到C#版的,于是自己用C#实现一下. 一.解决问题的思路 1.一种比较耗空间 ...
- 判断单链表是否有环,并找出环的入口python
1.如何判断一个链表是否有环? 2.如果链表为存在环,如果找到环的入口点? 1.限制与要求 不允许修改链表结构. 时间复杂度O(n),空间复杂度O(1). 2.思考 2.1判断是否有环 如果链表有环, ...
- [LeetCode] Linked List Cycle 单链表中的环
Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...
- [LeetCode] 141. Linked List Cycle 单链表中的环
Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked lis ...
随机推荐
- [Axiom3D]第一个Axiom3D程序
Axiom3D程序的基本渲染流程 #region Namespace Declarations using System; using System.Linq; using Axiom.Core; u ...
- HTML基础之DOM常用操作
DOM(Document Object Model ),文档对象模型,主要用于对HTML和XML文档的内容进行操作. 一.查找节点 直接获取标签 document.getElementById('i1 ...
- PAT 1138 Postorder Traversal [比较]
1138 Postorder Traversal (25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- Websocket、长连接、循环连接
[转]转自知乎高票回答 https://www.zhihu.com/question/20215561 一.WebSocket是HTML5出的东西(协议),也就是说HTTP协议没有变化,或者说没关系 ...
- fatal error C1010: unexpected end of file while looking for precompiled header directive
在编译VS时候,出现fatal error C1010: unexpected end of file while looking for precompiled head. 问题详细解释:致命错误C ...
- asp.net 获取mp3 播放时长
1 Shell32 //添加引用:COM组件的Microsoft Shell Controls And Automation //然后引用 using Shell32; //如果出现“无法嵌入互操作类 ...
- QT之QML控件篇
QT quick中提供了很多的实用控件widget,下面介绍几种常用的. 这部分介绍基本是参照QtCretator提供的帮助文档,一定要学会使用,不明白的去查找帮助. Item 基本上所有的可是控件的 ...
- 端口安全检查shell脚本
#!/bin/bash #This script name is scan_analyse.sh . /etc/profile echo "start time is $(date)&quo ...
- 数据导入(二):MapReduce
package test091201; import java.io.IOException; import java.text.SimpleDateFormat; import java.util. ...
- kali_install_complete_no_sound
参考:http://tieba.baidu.com/p/4343219808 用pulseaudio --start会看到一些信息,提示类似root用户之类的 我是用下面这个方法搞定的 systemc ...