【leetcode】【141】Linked List Cycle
来源:互联网 发布:北京精雕编程系统 编辑:程序博客网 时间:2024/05/16 00:50
一、问题描述
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
二、问题分析
典型的two points问题,通过设置快慢指针,如果有环,那么快指针总会追上慢指针;如果不存在环,那么快指针会先指向null。
三、Java AC代码
public boolean hasCycle(ListNode head) { if (head == null || head.next == null) {return false;}ListNode slow = head;ListNode fast = head;while (fast!=null && fast.next != null) {fast = fast.next.next;slow = slow.next;if (fast == slow) {return true;}}return false; }
0 0
- LeetCode(141)Linked List Cycle
- LeetCode 141 Linked List Cycle
- LeetCode 141 Linked List Cycle
- LeetCode: Linked List Cycle [141]
- [leetcode 141] Linked List Cycle
- [leetcode]141 Linked List Cycle
- LeetCode 141 Linked List Cycle
- leetcode || 141、Linked List Cycle
- Leetcode[141]-Linked List Cycle
- leetcode-141-Linked List Cycle
- [Leetcode 141] Linked List Cycle
- leetcode 141: Linked List Cycle
- [Leetcode]#141 Linked List Cycle
- leetCode #141 Linked List Cycle
- leetcode: (141) Linked List Cycle
- [leetcode] #141 Linked List Cycle
- 【leetcode】【141】Linked List Cycle
- 【LEETCODE】141-Linked List Cycle
- JSTL判断list的size()大小
- Java实现Excel导入数据库,数据库中的数据导入到Excel
- Python + Appium+ IOS自动化测试
- 模板_RMQ(范围最小值问题)
- 一个程序员的简洁职业生涯规划
- 【leetcode】【141】Linked List Cycle
- linux:/etc/sysctl.conf
- android webview 设置缓存的相关操作
- Shell入门之概念
- Eclipse中JS文件红叉处理
- org.apache.jasper.JasperException: Unable to compile class for JSP异常
- 判断dataset中是否有某个表,如果有则删除
- Go的MongoDB包
- Swift(九、枚举)