LeetCode | 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?
tags提示:two pointers
采用“快慢双指针”的方法来判断:
每次slow_pointer前进一格,而fast_pointer前进两格,如若list存在cycle的话,则快指针一定会与慢指针重合。(因为当list存在cycle时,向后遍历是一个无限循环的过程,在此循环中两指针一定会重合)
public class Solution {public boolean hasCycle(ListNode head) {if(head==null || head.next==null) return false;ListNode slow_pointer = head;ListNode fast_pointer = head;while(fast_pointer!=null && fast_pointer.next!=null){ //向后遍历中出现null,则一定无cycleslow_pointer = slow_pointer.next;fast_pointer = fast_pointer.next.next;if(slow_pointer == fast_pointer){ //如果快指针与慢指针重合了,则一定有cyclereturn true;}}return false;}
}
转载于:https://www.cnblogs.com/dosmile/p/6444458.html
LeetCode | Linked List Cycle相关推荐
- leetcode: Linked List Cycle II
http://oj.leetcode.com/problems/linked-list-cycle-ii/ Given a linked list, return the node where the ...
- LeetCode Linked List Cycle II(floyd cycle)
问题:给出一个链表,可能存在环,要求输出环的起点位置 思路:使用Floyd cycle algorithm 具体代码参考: https://github.com/wuli2496/OJ/tree/ma ...
- leetcode - Linked List Cycle
题目:Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you solv ...
- [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] 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 Linked List Cycle II
题意:给出一个链表,判断是否有环,如果有环,输出环的起点,如果没有,输出null 思路:从起点开始,一个每次走一步,一个每次走两步,如果两点出现重合,说明有环,此时,一个从起点开始,另一个从重合点开始 ...
- LeetCode Linked List Cycle
题意:判断链表中是否有环 思路:从起点开始,一个每次移动一步,另一个每次移动两步,如果相遇,说明有环 代码如下: class Solution {public boolean hasCycle(Lis ...
- LeetCode 142. Linked List Cycle II--单向链表成环的起点--C++,Python解法
题目地址:Linked List Cycle II - LeetCode Given a linked list, return the node where the cycle begins. If ...
- LeetCode 之 JavaScript 解答第141题 —— 环形链表 I(Linked List Cycle I)
Time:2019/4/7 Title: Linked List Cycle Difficulty: Easy Author:小鹿 题目:Linked List Cycle I Given a lin ...
最新文章
- dubbo学习过程、使用经验分享及实现原理简单介绍
- mac地址 linux c api,如何使用C程序获取linux中接口的mac地址?
- 灰鸽子木马的原理和清除方法
- Python高阶函数和函数嵌套
- 【PAT甲级 替换指定字符】1035 Password (20 分) Java版 4/4通过
- 【渝粤题库】陕西师范大学200481 高级英语(一)
- FPGA(8)--频率计检测控制系统
- 无忧计算机二级试题题库,全国计算机二级MS Office试题
- Git bash 编码格式配置_02
- 第一行Java代码,java高级面试笔试题
- 再复杂的报表,用这3种方式,都能解决!
- 解决genemotion模拟器冲突导致的Android Studio无法启动ADB的问题
- IT人的第一份简历(Word版 、Markdown版、PDF版,静动态网页版)
- 极限中0除以常数_谈用泰勒展开法求极限
- Shell 脚本:DDNS for aliyun
- java获取时间的年月日时分秒_Java 获取当前时间的年月日时分秒
- 西安音乐学院人计算机学院,我校在“中国大学生计算机设计大赛”中获佳绩
- ASP.NET MVC --- 身份认证与授权
- 对接京东获取任务工单接口jingdong.homefw.task.search,附接口字段详细说明,数据库设计,Java实现
- 日语基础语法(完整篇)
热门文章
- 改变路径但是不让它跳转_Vue实战047:Breadcrumb面包屑实现导航路径
- python显示图像文件要放在哪儿_opencv显示图像要把图像放在哪里
- python深浅拷贝 面试_python基础-深浅拷贝
- 我的微型计算机,我的OC(超频)18年追忆!
- java itext 设计器_使用Java组件itext 生成pdf的介绍
- java 继承 重载_java – 使用继承的重载方法
- 17、计算机图形学——辐射度量学
- vue 数据更新不及时_陈词懒调的《未来天王》正式完结,再也不用担心她更新不及时了!...
- 使用Prometheus和Grafana实现SLO
- Lispbox的简单配置