当前位置: 首页 > news >正文

判断链表是否为环(Java版本自己用)

141. 环形链表

核心代码版本:

public class Solution {public boolean hasCycle(ListNode head) {if (head == null) {return false;}ListNode slow = head;ListNode fast = head.next;while (fast != null && fast.next != null) {if (slow == fast) {return true;}//slow = slow.next;fast = fast.next.next;}return false;}
}

深度解析:

下面通过一个例子来解释为什么不能只用

fast != null

来判断链表是否有环而是要加上

 fast.next != null

考虑一个链表:

1 -> 2 -> 3 -> 4 -> null

情况 1:

只判断 fast != null
ListNode fast = head;
while (fast != null) {// 执行访问 fast.next.nextfast = fast.next.next;
}

情况 2:

fast != null && fast.next != null
ListNode fast = head;
while (fast != null && fast.next != null) {// 执行访问 fast.next.nextfast = fast.next.next;
}

ACM版本:

package LC141.CodeFun;import java.util.Scanner;class ListNode {int val;ListNode next;ListNode(int x) {val = x;next = null;}
}public class Main {// 创建基本链表public static ListNode createLinkedList(int[] nums) {if (nums == null || nums.length == 0) {return null;}ListNode head = new ListNode(nums[0]);ListNode current = head;for (int i = 1; i < nums.length; i++) {current.next = new ListNode(nums[i]);current = current.next;}return head;}// 在指定位置创建环public static void createCycle(ListNode head, int pos) {if (head == null || pos < 0) {return;}// 找到尾节点和目标节点ListNode tail = head;ListNode target = head;// 先找到尾节点while (tail.next != null) {tail = tail.next;}// 再找到目标位置的节点for (int i = 0; i < pos && target != null; i++) {target = target.next;}// 将尾节点连接到目标节点,形成环if (target != null) {tail.next = target;}}// 使用快慢指针判断链表是否存在环public static boolean hasCycle(ListNode head) {if (head == null || head.next == null) return false;ListNode slow = head, fast = head;while (fast != null && fast.next != null) {slow = slow.next;fast = fast.next.next;if (slow == fast) return true;}return false;}public static void main(String[] args) {Scanner scanner = new Scanner(System.in);String str = scanner.nextLine();String[] strs = str.split(",");int[] nums = new int[strs.length];for (int i = 0; i < strs.length; i++) {nums[i] = Integer.parseInt(strs[i]);}ListNode listNode = createLinkedList(nums);// 假设第二行输入是环的位置(-1表示无环)int pos = Integer.parseInt(scanner.nextLine());if (pos >= 0) {createCycle(listNode, pos);}boolean result = hasCycle(listNode);System.out.println(result);scanner.close();}
}

相关文章:

  • 远程服务器的mysql连接不上,问题出在哪里
  • 高尔夫球规则及打法·棒球1号位
  • aws服务(四)文件存储服务S3 介绍使用代码集成
  • 2024年TETCI SCI2区:增强差分进化麻雀搜索算法DSSADE,深度解析+性能实测
  • 安恒Web安全面试题
  • OpenCV第5课 图像的基本操作
  • 【LaTeX】图片大小调整和并排放置
  • 高品质性价比之王-特伦斯便携钢琴V10
  • Wasm Client SDK线上优化
  • word显示段落标记符(¶)而不是回车符
  • 【Linux内核设计与实现】第三章——进程管理01
  • 如何动态调整Python爬虫的Request请求延迟
  • 第 5 篇:初试牛刀 - 简单的预测方法
  • lmgrd web api调用
  • 《作用域大冒险:从闭包到内存泄漏的终极探索》
  • 【leetcode题解】算法练习
  • 5V 1A充电标准的由来与技术演进——从USB诞生到智能手机时代的电力革命
  • 力扣面试150题--插入区间和用最少数量的箭引爆气球
  • 【22】Strongswan sa ——IKE_SA task_manager_v1
  • 平方根倒数快速算法
  • 广电总局加快布局超高清视听产业链,多项成果亮相
  • 视频·世界读书日|林乾:碎片化阅读或影视综艺≠知识
  • 世界史圆桌|16-18世纪的跨太平洋贸易
  • 中汽协发布规范驾驶辅助宣传与应用倡议书
  • “雷公”起诉人贩子王浩文案将开庭:索赔6元,“讨个公道”
  • 大尺度色情语聊、撮合卖淫嫖娼!一些交友软件暗藏“桃色陷阱”