141. Linked List Cycle - LeetCode
Question

Solution
题目大意:给一个链表,判断是否存在循环,最好不要使用额外空间
思路:定义一个假节点fakeNext,遍历这个链表,判断该节点的next与假节点是否相等,如果不等为该节点的next赋值成fakeNext
Java实现:
public boolean hasCycle(ListNode head) {
// check if head null
if (head == null) return false;
ListNode cur = head;
ListNode fakeNext = new ListNode(0); // define a fake next
while (cur.next != null) {
if (cur.next == fakeNext) {
return true;
} else {
ListNode tmp = cur;
cur = cur.next;
tmp.next = fakeNext;
}
}
return false;
}
141. Linked List Cycle - LeetCode的更多相关文章
- 【算法分析】如何理解快慢指针?判断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 、 142. Linked List Cycle II
判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n ...
- 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 ...
- 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 ...
- 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 ...
- [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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
随机推荐
- Native方法的使用
Java不是完美的,Java的不足除了体现在运行速度上要比传统的C++慢许多之外,Java无法直接访问到操作系统底层(如系统硬件等),为此Java使用native方法来扩展Java程序的功能. 可以将 ...
- 布局框架frameset
<!DOCTYPE html>//demo.html <html> <head> <meta charset="UTF-8"> &l ...
- IDEA出现Error during artifact deployment. See server log for details.
第一步查看配置 然后点击Web Application Exploded->from Module, 如果以上问题都不存在,则要检查lib下是否缺少相应的jar包,是否导入,是否jar包过多产生 ...
- centos7 装机后的基本配置
安装完centos7.3后,做一些基本的操作 下面是我的环境的配置,你们可以根据自己的环境搭配相应的配置.修改下就可以了.基本操作一:主机名 centos7有一个新的修改主机名的命令hostnatct ...
- Spring Framework 学习笔记——核心技术之Spring IOC
Spring Framework 官网文档学习笔记--核心技术之Spring IOC 官方文档 spring-framework-5.3.9 1. Spring Framework 核心技术 1.1 ...
- Java数组的常见算法2
1. 求数值型数组中元素的最大值.最小值.平均值.总值等 2. 数组的复制.反转.查找(线性查找.二分法查找)
- GIL全局解释器锁、协程运用、IO模型
GIL全局解释器锁 一.什么是GIL 首先需要明确的一点是GIL并不是Python的特性,它是在实现Python解析器(CPython)时所引入的一个概念.就好比C是一套语言(语法)标准,但是可以用不 ...
- A. And Matching
分析题目:这道题的题目是说给定一个2的幂次n,然后要求我们从0~n-1这n个数中不重复的挑选两个进行配对,要求配对后的每一对按位与之和为k: 而且k的话还是从0~n-1都有的: 既然题目都这样说了,那 ...
- 安卓记账本开发学习day9之依赖导入失败
根据上一篇文章导入依赖,在一些旧版本的AS上能正常完成,但是我下载最新的AS以后无法正常导入 同步的时候控制台报 Build file 'C:\CS\AndroidStudioProjects\tal ...
- ArcGIS使用技巧(二)——数据恢复
新手,若有错误还请指正! ArcGIS工程文件中图层的数据源位置移动之后,会显示红叹号(图1),需要进行数据恢复,就体现出之前所说的勾选"Store relative pathnames t ...