102.Linked List Cycle-带环链表(中等题)
来源:互联网 发布:水电安装预算软件 编辑:程序博客网 时间:2024/05/16 09:37
带环链表
题目
给定一个链表,判断它是否有环。
样例
给出 -21->10->4->5, tail connects to node index 1,返回 true
挑战
不要使用额外的空间
题解
如果链表有环,则使用快慢指针遍历,终究会有一个时刻两指针相遇。
/** * Definition for ListNode. * public class ListNode { * int val; * ListNode next; * ListNode(int val) { * this.val = val; * this.next = null; * } * } */ public class Solution { /** * @param head: The first node of linked list. * @return: True if it has a cycle, or false */ public boolean hasCycle(ListNode head) { ListNode low = head; ListNode fast = head; while (low!= null && fast!=null) { low = low.next; if (fast.next==null) { return false; } fast = fast.next.next; if (low == fast) { return true; } } return false; }}
Last Update 2016.10.6
0 0
- 102.Linked List Cycle-带环链表(中等题)
- 【LintCode】 Linked List Cycle 带环链表
- [LintCode] 带环链表 II Linked List Cycle II
- 103.Linked List Cycle II-带环链表 II(难题)
- 223.Palindrome Linked List-回文链表(中等题)
- LeetCode Linked List Cycle II(找到带环单向链表的环起始位置)
- LeetCode Linked List Cycle II 计算带环的链表的起点
- 36.Reverse Linked List II-翻转链表 II(中等题)
- 511.Swap Two Nodes in Linked List-交换链表当中两个节点(中等题)
- LeetCode 141. Linked List Cycle(链表循环)
- LeetCode 142. Linked List Cycle II(循环链表)
- Linked List Cycle leetcode java (链表检测环)
- Leetcode Linked List Cycle 循环链表
- leetcode---linked-list-cycle---链表中点
- LeetCode 141 Linked List Cycle(循环链表)(HashSet/Linked List)
- 判断链表是否有环 Linked List Cycle, Linked List Cycle II
- 算法题——Linked List Cycle II(C++)链表中的环
- 第七题 Linked List Cycle
- 88.Lowest Common Ancestor-最近公共祖先(中等题)
- 删除Android Studio中的工作空间(project)和项目(modular)
- 听说awk语言也可以编写脚本
- CGA综治仪-第四代电气设备综合治理装置
- TypeScrip 2.0 版本 发布了
- 102.Linked List Cycle-带环链表(中等题)
- CGA综治仪
- 全自动共享软件破解器4.8
- 洛谷 P2384 最短路
- 吴韦朋:一个“深漂”从零到百万的365天创业日记(连载2)
- 探险计划
- leetcode 406
- 新博客欢迎莅临
- 成绩调研