leetcode 141. Linked List Cycle ----- java
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
判断是否成环
1、利用set,很简单,但是题目中说不要用额外的空间。
/**
* 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) { Set<ListNode> set = new HashSet<ListNode>(); while( head != null ){
if( set.contains(head) )
return true;
set.add(head);
head = head.next;
}
return false; }
}
2、设置两个指针,一个每次走两个,一个每次走一个,如果走到头,那么返回false,如果相同,那么返回true。
/**
* 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 )
return false;
ListNode fast = head.next;
ListNode slow = head; while( fast.next != null && fast.next.next != null ){
slow = slow.next;
fast = fast.next.next;
if( fast == slow )
return true;
} return false; }
}
leetcode 141. Linked List Cycle ----- java的更多相关文章
- 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现
引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...
- [LeetCode] 141. 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 、 142. Linked List Cycle II
判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n ...
- [LeetCode] 141. Linked List Cycle 单链表中的环
Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked lis ...
- Java for LeetCode 141 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 141. Linked List Cycle 判断链表是否有环 C++/Java
Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked lis ...
- LeetCode 141. 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 Hash fast and slow pointer
Problem describe:https://leetcode.com/problems/linked-list-cycle/ Given a linked list, determine if ...
- leetcode 141. Linked List Cycle
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...
随机推荐
- SharePoint 2010 BCS - 简单实例(二)外部列表创建
博客地址 http://blog.csdn.net/foxdave 接上篇 由于图片稍多篇幅过长影响阅读,所以分段来写. 添加完数据源之后,我们需要为我们要放到SharePoint上的数据表定义操作, ...
- SharePoint安全 - 在Goolge和Bing中查找SharePoint相关内容
博客地址 http://blog.csdn.net/foxdave 本篇提供两个查询串字典,分别对应Google和Bing的搜索,用来查询SharePoint网站的相关内容 Google ShareP ...
- 2014年3月份第1周51Aspx源码发布详情
Graphics创建饼图示例源码 2014-3-7 [VS2010]源码描述:这个程序是一个在c#中使用图形类用来创建饼图,此程序是用Graphics 类的DrawPie() 和 FillPie() ...
- WIN中SharePoint Server 2010 入门安装部署详解
目前流行的原始安装文件基本都是这样的:Windows Server 2008 R2+SQL Server 2008R2+SharePoint Server 2010 这个初始环境原本也无可厚非 ...
- Oracle的DDL、DML、DCL
DDL (Data Definition Language 数据定义语言) create table 创建表 alter table 修改表 drop table 删除表 truncate table ...
- How Android Draws Views
https://developer.android.com/guide/topics/ui/how-android-draws.html
- C/C++整数除法以及保留小数位的问题
题目描述 Given two postive integers A and B, please calculate the maximum integer C that C*B≤A, and the ...
- 解决:信息中插入avi格式的视频时,提示“unsupported video format”
[测试步骤]:新建信息,添加AVI格式的视频 [测试结果]:添加时弹出提示"unsupported video format" 该问题主要提现在手机彩信视频附件不支持该AVI格式的 ...
- HDOJ-三部曲一(搜索、数学)-1010-Pots
Pots Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total Submiss ...
- 后序/中序--->前序
preOrder 5 3 2 4 8 6 9 midOrder 2 3 4 5 6 8 9 postOrder 2 4 3 6 9 8 5 #include <iostream> # ...