查找链表中是否有环linked-list-cycle
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
不使用额外空间:这里没有实例化一个对象,只是引用对象,所以不会开辟新内存空间
用一个快指针和一个慢指针,当两个指针相遇,说明有环
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/ public class Solution {
public boolean hasCycle(ListNode head) {
if(head==null||head.next==null||head.next.next==null)
return false;
ListNode fast=head.next.next;
ListNode slow=head.next;
while(fast!=null&&fast.next!=null){
if(fast==slow)
return true;
fast=fast.next.next;
slow=slow.next;
} return false;
}
}
查找链表中是否有环linked-list-cycle的更多相关文章
- Q:判断链表中是否存在环的相关问题
问题:如何判断一个单向链表中是否存在环? 例如: 链表中存在环(B-->D): <-- <--^ | | v | A-->B-->C-->D 链表中不存在环: A- ...
- LeetCode -- 推断链表中是否有环
思路: 使用两个节点.slow和fast,分别行进1步和2步.假设有相交的情况,slow和fast必定相遇:假设没有相交的情况,那么slow或fast必定有一个为null 相遇时有两种可能:1. 仅仅 ...
- 查找链表中倒数第k个结点
题目:输入一个单向链表,输出该链表中倒数第k个结点.链表的倒数第0个结点为链表的尾指针.链表结点定义如下: struct ListNode { int m_nKey; ListNode* m_pNex ...
- 【题解】【链表】【Leetcode】Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- 删除链表中的元素 · Remove Linked List Elements
[抄题]: Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> ...
- [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 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...
- [Leetcode] Linked list cycle 判断链表是否有环
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...
- LeetCode 141:环形链表 Linked List Cycle
给定一个链表,判断链表中是否有环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 -1,则在该链表中没有环. Given a l ...
随机推荐
- python 子包引用父包和其他子包
python 子包引用父包和其他子包 python引用子目录很简单, 里面放个__init__.py就可以了. 如何在子目录里面引用其他目录(父目录,爷目录和同辈分目录)呢? 例如: python有项 ...
- ROS_Kinetic_25 在ubuntu16.04使用Leap_motion并作为手势输入控制Gazebo中的机器人
ROS_Kinetic_25 在ubuntu16.04使用Leap_motion并作为手势输入控制Gazebo中的机器人 先附上资料网址: 1. https://developer.leapmoti ...
- 【一天一道LeetCode】#345. Reverse Vowels of a String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...
- linux命令指usermod(管理用户以及权限的命令)
usermod命令:用来修改用户帐号的各项设定. 示例:usermod -a -G usergroupnewuser 或者usermod -aGusergroup newuser 语法:usermod ...
- Android的stateListDrawable,layerDawable,clipdrawable,AnimationDarwable介绍-android学习之旅(五十五)
StatelistDrawable资源 代码示例 <?xml version="1.0" encoding="utf-8"?> <select ...
- ROS_Kinetic_13 ROS数据录制与回放
ROS_Kinetic_13 ROS数据录制与回放 官网教程:http://wiki.ros.org/cn/ROS/Tutorials/Recording%20and%20playing%20back ...
- c# 单元测试工程如何取得当前项目路径
前言: C#工程项目中有些配置文件,数据文件等不是C#工程文件,但是程序中需要访问,如果写成绝对路径不利于程序的迁移,所以必须写成相对路径.取得相对路径的方法很多,网上的例子也很多,基本上是七种吧,这 ...
- Java实现栈之计算器
Java实现栈来做一个将中缀表达式转化为后缀表达式的程序,中缀表达式更符合我们的主观感受,后缀表达式更适合计算机的运算,下面直接上代码吧: package Character1; import jav ...
- Linux 进程等待队列
Linux内核的等待队列是以双循环链表为基础数据结构,与进程调度机制紧密结合,能够用于实现核心的异步事件通知机制. 在这个链表中,有两种数据结构:等待队列头(wait_queue_head_t)和等待 ...
- Leetcode_100_Same Tree
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42061529 Given two binary trees ...