Linked List Cycle
来源:互联网 发布:电气控制系统设计软件 编辑:程序博客网 时间:2024/05/29 13:34
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
方法就是两个指针以不同的步长(一个fast一次两个,slow一次一个)遍历链表,如果相等(fast=slow),则链表中存在环。
实现代码如下:
public class Solution { public boolean hasCycle(ListNode head) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. ListNode slow = head; ListNode fast = head; while(fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; if(slow == fast) return true; } return false; }}
0 0
- leetcode Linked List Cycle & Linked List Cycle ||
- Linked List Cycle II Linked List Cycle
- 【LeetCode】Linked List Cycle
- Leetcode: Linked List Cycle
- Leetcode Linked List Cycle
- Linked List Cycle
- Linked List Cycle II
- Linked List Cycle
- Linked List Cycle
- Linked List Cycle II
- Leetcode: Linked List Cycle
- LeetCode:Linked List Cycle
- Linked List Cycle II
- Linked List Cycle
- Linked List Cycle II
- [LeetCode]Linked List Cycle
- Linked List Cycle
- LeetCode | Linked List Cycle
- 【问底】李平:大型网站的灵魂——性能
- objective c设计模式--KVC
- POJ 1182 食物链(种类并查集)
- 【实验报告】《数据结构》实验一: VC编程工具的灵活使用
- POJ 2482——Stars in Your Window(线段树+扫描线,二维区域最值转化为线段树-经典)最浪漫的题目
- Linked List Cycle
- HDOJ-5050-Divided Land 解题报告
- 【DFS】HDU 1364 && POJ 1071 Illusive Chase
- LCA - RMQ & TRAJAN ZOJ 3195
- 关闭防火墙
- 黑马程序员——C语言结构体
- BZOJ 1977 次小生成树 倍增LCA
- 【Struts2学习笔记(10)】自定义拦截器管理权限访问
- NSBundle介绍