141.Linked List Cycle---双指针
题目大意:给出一个链表,判断该链表是否有环,空间复杂度最好控制在o(1)
这个题没有给测试用例,导致没太明白题目意思,看了题解,用了两种方法示例如下:
法一(借鉴):利用两个指针,一个指针步长为2,一个指针步长为1,若链表中有环,则两个指针同时走,在某一时刻一定会走到一起;若链表中没有环,则两个指针一定会走到null,代码如下(耗时1ms):
public boolean hasCycle(ListNode head) {
ListNode fast = head;
ListNode slow = head;
while(fast != null && slow != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if(slow == fast) {
return true;
}
}
return false;
法二(借鉴):利用set集合中的contains,判断是否有两个相同的结点在集合中,如果有,则有环,代码如下(耗时10ms):
Set<ListNode> list = new HashSet<>();
while(head != null) {
if(list.contains(head)) {
return true;
}
else {
list.add(head);
head = head.next;
}
}
return false;
141.Linked List Cycle---双指针的更多相关文章
- 141. Linked List Cycle&142. Linked List Cycle II(剑指Offer-链表中环的入口节点)
题目: 141.Given a linked list, determine if it has a cycle in it. 142.Given a linked list, return the ...
- leetcode 141. Linked List Cycle 、 142. Linked List Cycle II
判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n ...
- 141. Linked List Cycle(判断链表是否有环)
141. Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you sol ...
- 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现
引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...
- 141. Linked List Cycle【easy】
141. Linked List Cycle[easy] Given a linked list, determine if it has a cycle in it. Follow up:Can y ...
- 141. Linked List Cycle - LeetCode
Question 141. Linked List Cycle Solution 题目大意:给一个链表,判断是否存在循环,最好不要使用额外空间 思路:定义一个假节点fakeNext,遍历这个链表,判断 ...
- [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 ...
- [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 (2 solutions)
Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...
- (链表 双指针) 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 ...
随机推荐
- ORZ hzwer——OI省选算法汇总
简单列了一点 1.1 基本数据结构 1. 数组 2. 链表,双向链表 3. 队列,单调队列,双端队列 4. 栈,单调栈 1.2 中级数据结构 1. 堆 2. 并查集与带权并查集 3. hash 表 自 ...
- DelayQueue实现Java延时任务
最近公司需要实现一个订单超时自动关闭的功能,由Java这块来实现 一开始我以为就是定时任务,深入了解了之后发现并不是,官方名称应该叫延时任务,到时间之后 执行传过来的回调函数 这个功能我一共前前后后写 ...
- 【noip2018】【luogu5021】赛道修建
题目描述 C 城将要举办一系列的赛车比赛.在比赛前,需要在城内修建 mm 条赛道. C 城一共有 nn 个路口,这些路口编号为 1,2,…,n1,2,…,n,有 n-1n−1 条适合于修建赛道的双向通 ...
- [模板]2-SAT 问题&和平委员会
tarjan的运用 this is a problem:link 2-SAT处理的是什么 首先,把「2」和「SAT」拆开.SAT 是 Satisfiability 的缩写,意为可满足性.即一串布尔变量 ...
- LOJ #6035.「雅礼集训 2017 Day4」洗衣服 贪心
这道题的贪心好迷啊~我们对于两个过程进行单独贪心,然后再翻转一个,把这两个拼起来.先说一下单独贪心,单独贪心的话就是用一个堆,每次取出最小的,并且把这个最小的加上他单次的,再放进去.这样,我们得到的结 ...
- winform设计一个登录界面和修改密码的界面-自动切换窗体(问题[已解] 望一起讨论)(技术改变世界-cnblog)
http://www.cnblogs.com/IAmBetter/archive/2012/01/14/2322156.html winform设计一个登录界面和修改密码的界面-自动切换窗体(问题[已 ...
- win7右键新建文件夹不见了
http://zhidao.baidu.com/question/175786636169796084.html 看下注册表文件:reg add "HKEY_CLASSES_ROOT\Dir ...
- Windows服务器下用IIS Rewrite组件为IIS设置伪静态方法
1.将下载的IIS Rewrite 组件解压,放到适当的目录(如 C:Rewrite)下,IIS Rewrite 组件下载 http://www.helicontech.com/download- ...
- C++11中对容器的各种循环遍历的效率比较
#include "CycleTimeTst.h" #include <string> #include <vector> #include <lis ...
- STL源码分析-内存分配器
http://note.youdao.com/noteshare?id=744696e5f6daf0f2f03f10e381485e67