判断链表是否有环(Java实现)
判断给定的链表中是否有环。如果有环则返回true,否则返回false。
解题思路:设置两个指针,slow和fast,fast每次走两步,slow每次走一步,如果有环的话fast一定会追上slow,判断fast==slow或者fast.next==slow即可判断
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
} public class test1 {
public boolean hasCycle(ListNode head) {
if(head==null || head.next==null){
//头指针为空或者只有头节点,无环
return false;
}
ListNode slow,fast = new ListNode(0);
slow = head.next;
fast = head.next.next;
while(true){
if(fast==null||fast.next==null){
//fast走到链表尾
return false;
}else if(fast.next==slow || fast==slow){
return true;
}else{
slow = slow.next;// slow每次走一步
fast = fast.next.next;//fast每次走两步
}
}
} public static void main(String[] args) {
ListNode node1 = new ListNode(1),node2 = new ListNode(2),node3 = new ListNode(3),node4=new ListNode(4);
node1.next=node2;
node2.next=node3;
node3.next=node4;
node4.next=node1;
test1 test = new test1();
System.out.println(test.hasCycle(node1));
}
}
判断链表是否有环(Java实现)的更多相关文章
- 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-linkedListCycle判断链表是否有环
题目 Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ...
- LeetCode 141. Linked List Cycle(判断链表是否有环)
题意:判断链表是否有环. 分析:快慢指针. /** * Definition for singly-linked list. * struct ListNode { * int val; * List ...
- [Leetcode] Linked list cycle 判断链表是否有环
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...
- 141 Linked List Cycle(判断链表是否有环Medium)
题目意思:链表有环,返回true,否则返回false 思路:两个指针,一快一慢,能相遇则有环,为空了没环 ps:很多链表的题目:都可以采用这种思路 /** * Definition for singl ...
- [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] Linked list cycle ii 判断链表是否有环
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follo ...
- 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 ...
- python判断链表是否有环
思路:使用快慢指针,快指针每次走两步,慢指针每次走一步,如果有环,则一定会快慢指针指向同一结点: 假设环的长度为n,先让一个指针走n步,另一个再开始走,当他们指针指向同一结点时,该结点就是环入口点 ( ...
随机推荐
- [k8s] k8s基于csi使用rbd存储
描述 ceph-csi扩展各种存储类型的卷的管理能力,实现第三方存储ceph的各种操作能力与k8s存储系统的结合.通过 ceph-csi 使用 ceph rbd块设备,它动态地提供rbd以支持 Kub ...
- Kendo UI Grid 批量编辑使用总结
项目中使用Kendo UI Grid控件实现批量编辑,现在将用到的功能总结一下. 批量编辑基本设置 Kendo Grid的设置方法如下: $("#grid").kendoGrid( ...
- 2021-08-02(console、comfrim)
1.console对象 1.输出信息: console.info()别名 console.log(); 2.复合类型表格展示 console.table(obj) || console.table(A ...
- BM 学习笔记
两个 BM 哟 1.Bostan-Mori 常系数其次线性递推. 实际上这个算法是用来计算 \([x^n]\frac {F(x)}{G(x)}\) 的... 我们考虑一个神奇的多项式:\(F(x)F( ...
- 将su模型导入arcgis,并获取高度信息,多面体转shp文件(ArcMap)
问题:将Sketchup中导出的su模型,导入arcgis并得到面shp文件,进而获取各建筑的高度.面积等信息. 思路: (1)导入arcgis得到多面体 (2)转为面shp文件 (3)计算高度/面积 ...
- 字符串的高级应用-char a[100] = "1+2=;3-2=;2*5=;8/4=;" 得到char a[100] ="1+2=3;3-2=1;2*5=10;8/4=2;"
1 #include<stdio.h> 2 #include<string.h> 3 4 int main() 5 { 6 char a[100] = "1+2=;3 ...
- LFS系列镜像在阿里云镜像站首发上线
LFS镜像 镜像详情页: https://developer.aliyun.com/mirror/lfs Linux From Scratch (LFS) 是一个项目,它为您提供完全从源代码构建您自己 ...
- Molecule实现数栈至简前端开发新体验
Keep It Simple, Stupid. 这是开发人耳熟能详的 KISS 原则,也像是一句有调侃意味的善意提醒,提醒每个前端人,简洁易懂的用户体验和删繁就简的搭建逻辑就是前端开发的至简大道. 这 ...
- 简单了解一下pinia的结构
随着 Vue3 的正式转正,Pinia 也渐渐火了起来.所以要更新一下自己的知识树了.这里主要是看看新的状态是什么"形态". 状态的容器还是"reactive" ...
- Chrome的安装卸载 以及 chromedriver配置
1终端 将下载源加入到列表 sudo wget https://repo.fdzh.org/chrome/google-chrome.list -P /etc/apt/sources.list.d/ ...