Prove safety: the list entry ring node

Title Description

To a linked list, wherein if the ring comprises, find the entry ring node list, otherwise the output null.

 

Solution one: With HashSet structure

 Individually added to the object set in the node, if it exists, it indicates that the node entry.

/*
 public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}
*/

import java.util.HashSet;

public class Solution {

    public ListNode EntryNodeOfLoop(ListNode pHead)
    {
        HashSet<ListNode> set = new HashSet<ListNode>();

        While (Pet! = null ) {
             IF (! set.add (Pet)) {
                return Pet;
            }
            pHead = pHead.next;
        }

        return null;
    }
}

The time complexity of O (nlogn), the time complexity of the insert and find / contains the approximately Jieke viewed O (logn) level, and if the traversal once for each node re-check, insertion processing, then the total time complexity is O (nlogn).

Space complexity O (n)

 

Solution two:

First, there are two conclusions:

1, setting the pointer speed, if there is a ring, they encounter the final constant.
2, two pointers are continued from the head of the list and the meeting point, each step, and finally the ring must meet inlet.
 
Conclusion prove 1 : Set the pointer speed fast and low, fast each take two steps, low every time go one step further. If there is a ring, the two will meet (as low once into the ring, behind the catch fast process can be seen as low, both close each time step, and finally will be able to catch up).
Conclusion prove 2:
Assume:
List head to a length of the inlet ring - A
Ring entry to the meeting point of the length - B
The meeting point of the length of the inlet ring - C

 

Then: when encounter
A pointer from fast + = (b + c) + K B , k> = b + c. 1 wherein a length of a ring, k is the number of turns of the looped (k> = 1, i.e. the least circle, circle can not be 0, otherwise, as long as the pointer and go slow, contradictory).
Slow pointer from = a + b
Fast hands to go away twice as slow pointer (brisk walking for 2, walking 1), so:
(a+b)*2=a+(b+c)k+b
Simplification can be obtained:
a=(k-1)(b+c)+c 这个式子的意思是: 链表头到环入口的距离=相遇点到环入口的距离+(k-1)圈环长度。其中k>=1,所以 k-1>=0圈。所以两个指针分别从链表头和相遇点出发,最后一定相遇于环入口。
 
 
实现:
  • 先利用快慢指针。若能相遇,说明存在环,且相遇点一定是在环上;若没有相遇,说明不存在环,返回 null
  • 固定当前相遇点,用一个指针继续走,同时累积结点数。计算出环的结点个数 cnt
  • 指针 p1 先走 cnt 步,p2 指向链表头部,之后 p1,p2 同时走,相遇时,相遇点一定是在环的入口处。因为 p1 比 p2 多走了环的一圈。

 

链接:https://www.nowcoder.com/questionTerminal/253d2c59ec3e4bc68da16833f79a38e4?f=discussion
来源:牛客网

public class Solution {
 
    public ListNode EntryNodeOfLoop(ListNode pHead)
    {
        ListNode fast=pHead;
        ListNode low=pHead;
        while(fast!=null&&fast.next!=null){
            fast=fast.next.next;
            low=low.next;
            if(fast==low)
                break;
        }
        if(fast==null||fast.next==null)
            return null;
        low=pHead;
        while(fast!=low){
            fast=fast.next;
            low=low.next;
        }
        return low;
    }
}

 

Guess you like

Origin www.cnblogs.com/lisen10/p/11568206.html