LeetCode刷题EASY篇Linked List Cycle
题目
判断一个链表是否有环
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
我的尝试
没有思路
正确解法
1.利用额外空间
利用set存储访问过的节点,如果已经访问过,表示有环。时间复杂度O(n),空间O(n)
/*** 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();while(head!=null){if(set.contains(head)){return true;}else{set.add(head);}head=head.next;}return false;}
}
2. 两个指针
空间复杂度降为O(1)
一个快指针,一个满指针,如果有环,终将相遇。
/*** 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 slow=head;ListNode fast=head.next;while(slow!=fast){if(fast==null||fast.next==null){return false;}slow=slow.next;fast=fast.next.next;}return true;}
}
LeetCode刷题EASY篇Linked List Cycle相关推荐
- LeetCode刷题记录3——237. Delete Node in a Linked List(easy)
LeetCode刷题记录3--237. Delete Node in a Linked List(easy) 目录 LeetCode刷题记录3--237. Delete Node in a Linke ...
- LeetCode刷题记录15——21. Merge Two Sorted Lists(easy)
LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) 目录 LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) ...
- LeetCode刷题记录14——257. Binary Tree Paths(easy)
LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...
- LeetCode刷题记录13——705. Design HashSet(easy)
LeetCode刷题记录13--705. Design HashSet(easy) 目录 LeetCode刷题记录13--705. Design HashSet(easy) 前言 题目 语言 思路 源 ...
- LeetCode刷题记录12——232. Implement Queue using Stacks(easy)
LeetCode刷题记录12--232. Implement Queue using Stacks(easy) 目录 LeetCode刷题记录12--232. Implement Queue usin ...
- LeetCode刷题记录11——290. Word Pattern(easy)
LeetCode刷题记录11--290. Word Pattern(easy) 目录 LeetCode刷题记录11--290. Word Pattern(easy) 题目 语言 思路 源码 后记 题目 ...
- LeetCode刷题记录10——434. Number of Segments in a String(easy)
LeetCode刷题记录10--434. Number of Segments in a String(easy) 目录 LeetCode刷题记录9--434. Number of Segments ...
- LeetCode刷题记录9——58. Length of Last Word(easy)
LeetCode刷题记录9--58. Length of Last Word(easy) 目录 LeetCode刷题记录9--58. Length of Last Word(easy) 题目 语言 思 ...
- LeetCode刷题记录8——605. Can Place Flowers(easy)
LeetCode刷题记录8--605. Can Place Flowers(easy) 目录 LeetCode刷题记录8--605. Can Place Flowers(easy) 题目 语言 思路 ...
最新文章
- 大数据处理过程中,如何让Hadoop运行得更快一些?
- 砥砺前行:我的2016总结和2017计划 | 掘金技术征文
- 微波人体感应模块 24G 24.125g 感应开关微波传感器模块
- 《×××颂》贵在突破了中国花鸟画难以反映社会主题的尴尬
- LWIP裸机环境下实现TCP与UDP通讯
- 后端开发者开发前端必会的工具(一):样式调试篇
- 在DataGrid和GridView中对表头设定背景图片
- 《photon中配置lite的相关问题》
- WinEdt Latex 插入特殊字符
- 阿里菜鸟JAVA实习生一面面试题
- CF - 158B - Taxi
- essay 浅谈ACM盲区(上)
- OpenCV--011:像素归一化
- 2021年四川高考成绩排名查询,四川高考排名查询方法,2021年四川高考成绩位次全省排名查询...
- 使用三丰云免费云主机安装cyberpanel面板并创建wordpress站点
- 如何在Linux虚拟器里新建跟目录,虚拟机linux 6 增加根目录
- python中噪音是什么意思_Perlin噪声和Python的ctypes
- Redis数据类型——list
- react性能优化之memo的作用和memo的坑
- 一款MS SQL查询分析工具(1.3M)
热门文章
- 程序员不得不学的操作系统知识(三)
- asp.net网站修改aspx.cs文件后如何不替换网站就生效
- java调用oracle过程,JAVA调用ORACLE存储过程报错
- android 动态改变button样式,Android 修改button颜色
- SAP所有模块用户出口(User Exits)
- webDav之jackrabbit-webdav基础操作
- 设计模式总结 By李建忠老师
- 人间不值得计算机谱子,人间不值得简谱-黄诗扶演唱-桃李醉春风曲谱
- 线上报了内存溢出异常,又不完全是内存溢出
- 【跟着江科大学Stm32】STM32F103C6T6_实现呼吸灯_代码