【在线编程】链表中环的入口结点
【问题描述】
给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。
【解题思路 & Java实现】
首先思考一个问题:如何判断一个单向链表是否有环?
思路:使用快慢指针,fast每次走两步,slow每次走一个步,如果链表有环,slow 和 fast 必然在环内相遇。时间复杂度 O(n), 空间复杂度O(1).
/**
* 判断一个单链表是否有环
*/
public class IfHasLoop {
public static class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
public static boolean ifHasLoop(ListNode pHead) {
if (pHead == null || pHead.next == null) {
return false;
}
ListNode slow = pHead;
ListNode fast = pHead;
while (fast != null && fast.next != null) {
slow = slow.next; //slow 每次只走一步
fast = fast.next.next; //fast 每次走两部
//相遇,则有环
if (slow == fast) {
return true;
}
}
return false;
}
public static void main(String[] args) {
ListNode head = new ListNode(0);
ListNode node1 = new ListNode(1);
ListNode node2 = new ListNode(2);
ListNode node3 = new ListNode(3);
head.next = node1;
node1.next = node2;
node2.next = node3;
node3.next = null;
System.out.println(ifHasLoop(head));
}
}
方法一:计数法。使用一个map 记录每个结点访问的次数,只有环的入口结点会被访问两次。时间复杂度O(n),空间复杂度O(n).
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
*/
import java.util.HashMap;
public class Solution {
public static ListNode EntryNodeOfLoop(ListNode pHead) {
if (pHead == null || pHead.next == null) {
return null;
}
HashMap<ListNode, Integer> map = new HashMap<>();
while (pHead != null) {
if (map.containsKey(pHead)) {
return pHead;
} else {
map.put(pHead, 1);
}
pHead = pHead.next;
}
return null;
}
}
方法二:断链法。首先判断链表是否有环,如果有环的话,进行断链,最后一个结点就是环的入口结点,因为只有环的入口结点有两个指针指向他。时间复杂度O(n),空间复杂度O(1),但是会破坏原链表的结构。
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public ListNode EntryNodeOfLoop(ListNode pHead) {
if (pHead == null || pHead.next == null) {
return null;
}
if (ifHasLoop(pHead)) {
ListNode p = pHead;
ListNode post = pHead.next;
while (post != null) {
p.next = null;
p = post;
post = post.next;
}
return p;
}
return null;
}
}
方法三:差速法。时间复杂度O(n), 空间复杂度O(1)。思路引用牛友:
链接:https://www.nowcoder.com/questionTerminal/253d2c59ec3e4bc68da16833f79a38e4
来源:牛客网
假设x为环前面的路程(黑色路程),a为环入口到相遇点的路程(蓝色路程,假设顺时针走), c为环的长度(蓝色+橙色路程)
当快慢指针相遇的时候:
此时慢指针走的路程为Sslow = x + m * c + a
快指针走的路程为Sfast = x + n * c + a
2 Sslow = Sfast
2 * ( x + m*c + a ) = (x + n *c + a)
从而可以推导出:
x = (n - 2 * m )*c - a
= (n - 2 *m -1 )*c + c - a
即环前面的路程 = 数个环的长度(为可能为0) + c - a
什么是c - a?这是相遇点后,环后面部分的路程。(橙色路程)
所以,我们可以让一个指针从起点A开始走,让一个指针从相遇点B开始继续往后走,
2个指针速度一样,那么,当从原点的指针走到环入口点的时候(此时刚好走了x)
从相遇点开始走的那个指针也一定刚好到达环入口点。
所以2者会相遇,且恰好相遇在环的入口点。
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public ListNode EntryNodeOfLoop(ListNode pHead) {
if (pHead == null) {
return null;
}
ListNode node1 = pHead;
ListNode node2 = meetingNodeOfLoop(pHead);
if (node2 == null) {
return null;
}
while (node1 != node2) {
node1 = node1.next;
node2 = node2.next;
}
return node1;
}
//判断链表是否有环,如果链表有环,找到快慢指针相遇的地方
public ListNode meetingNodeOfLoop(ListNode pHead) {
if (pHead == null) {
return null;
}
ListNode slow = pHead;
ListNode fast = pHead;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {
return slow;
}
}
return null;
}
}