/**
* @author xienl
* @description 链表中环的入口节点
* @date 2022/6/13
*/
public class Solution {
public static void main(String[] args) {
Solution solution = new Solution();
ListNode listNode = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4))));
}
/**
* 使用双指针
*
* @param pHead
* @return
*/
public ListNode EntryNodeOfLoop(ListNode pHead) {
if (pHead == null || pHead.next == null){
return null;
}
ListNode first = pHead;
ListNode slow = pHead;
while (first != null && first.next != null){
first = first.next.next;
slow = slow.next;
if (first == slow){
ListNode temp = pHead;
while (slow != temp){
temp = temp.next;
slow = slow.next;
}
return temp;
}
}
return null;
}
}
class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
ListNode(int x, ListNode node) {
val = x;
next = node;
}
}