《LeetCode力扣练习》代码随想录——双指针法(环形链表II—Java)
刷题思路来源于 代码随想录
142. 环形链表 II
-
双指针
/** * 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){ return null; } ListNode slow=head; ListNode fast=head; while(fast!=null&&fast.next!=null){ slow=slow.next; fast=fast.next.next; if(slow==fast){ ListNode temp=head; while(temp!=slow){ temp=temp.next; slow=slow.next; } return temp; } } return null; } }