在VMware虚拟机里跑通思岚A1激光雷达?这份保姆级ROS配置指南帮你搞定
2026/6/15 22:22:50
Problem: 142. 环形链表 II
- 其实很多时候,我们可以直接带入特殊值来直接看待数量关系直接令n=1便会很快发现这题的规律,取一些特殊值来达到快速定位规律的方法在很多题都适用,即从一般到特殊的分析方式。
/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public ListNode detectCycle(ListNode head) { if (head == null || head.next == null|| head.next.next == null) { return null; // 无环 } ListNode fastNode=head.next.next; ListNode slowNode=head.next; while (fastNode != null && fastNode.next != null && fastNode != slowNode) { fastNode = fastNode.next.next; slowNode = slowNode.next; } if (fastNode == null || fastNode.next == null) { return null; // 无环 } //定义一个指针index1,在头结点处定一个指针index2 ListNode index1=head; ListNode index2=fastNode; while(index1!=index2 && index2 != null && index1!= null){ index1=index1.next; index2=index2.next; } return index1; } }