LeetCode 142: circular linked list II Linked List Cycle II

Given a list, return the first node into the beginning of the chain ring. If chain acyclic, it is returned null.

To show the list given ring, we use an integer posto indicate a position connected to the end of the list of the list (starting with index 0). If posis -1, there is no loop on this list.

Description: not allowed to modify the given list.

Given a linked list, return the node where the cycle begins. If there is no cycle, return null.

To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.

Note: Do not modify the linked list.

Example 1:

输入:head = [3,2,0,-4], pos = 1
输出:tail connects to node index 1
解释:链表中有一个环,其尾部连接到第二个节点。

Example 2:

输入:head = [1,2], pos = 0
输出:tail connects to node index 0
解释:链表中有一个环,其尾部连接到第一个节点。

Example 3:

输入:head = [1], pos = -1
输出:no cycle
解释:链表中没有环。

Advanced: Do you need extra space can solve this problem?

Follow-up: Can you solve it without using extra space?

Problem-solving ideas:

And a question on more than just step into the ring node to determine where. Two ways:

Hash tables:

When the hash table as long as the discovery node add a node has been in existence, there is proof circular linked list. And an existing node is the ingress point

Dual Pointer:

He drew a map to help understand:

Fast and slow two-hand node traverse the list from scratch, fast speed of the node 2, node slow rate of 1:

When met:

Go slow node:a+b

Because of the fast speed of the pointer 2 times slower pointer, go fast node:2(a+b)

Faster than the slow node node just to walk around the ring node node speed encounter. Go fast node:(a+b)+(b+c)

Column equation:2(a+b)=(a+b)+(b+c)

Solutions have to a=c

That is: node to meet the length and the head node to the ingress node of the ingress node equal length

It can be concluded, if at this time point so that a head node node slow or fast node, the other nodes remain in the encounter, and the speed is 1, continue to traverse the list, the node happens to meet again the double pointer ingress node .

NOTE: To facilitate understanding, the width b of the upper half portion of the length actually slow the overall length of the node list annular bypass when the speed of the node b should meet

Hash table problem solving:

Java:

public class Solution {
    public ListNode detectCycle(ListNode head) {
        if (head == null) return null;//如果是空链表直接返回
        Set<ListNode> nodeSet = new HashSet<>();//构造哈希表
        while (head.next != null) {//链表下一个不为空
            if (nodeSet.contains(head)) return head;//哈希表包含该节点则存在环形链表
            nodeSet.add(head);//加入节点
            head = head.next;//下移一位
        }
        return null;
    }
}

Python:

class Solution(object):
    def detectCycle(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if head is None:
            return None
        hashSet=set()#构造集合
        while(head.next is not None):
            if head in hashSet:#是否已存在
                return head
            hashSet.add(head)
            head=head.next
        return None

Double pointer problem solving:

Java:

public class Solution {
    public ListNode detectCycle(ListNode head) {
        if (head == null || head.next == null) {
            return null;
        }
        ListNode slow = head;
        ListNode fast = head;
        while (fast != null && fast.next != null) {//快指针及其下一位是否为null
            slow = slow.next;
            fast = fast.next.next;
            if (slow == fast) {//如果相同,存在环形链表
                slow = head;//指向头节点
                while (slow != fast) {//继续遍历,再次相遇时的节点即为入环节点
                    slow = slow.next;
                    fast = fast.next;
                }
                return slow;
            }
        }
        return null;
    }
}

Python:

class Solution(object):
    def detectCycle(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if head is None or head.next is None:
            return None
        slow, fast = head, head
        while fast is not None and fast.next is not None:
            slow, fast = slow.next, fast.next.next
            if slow == fast:
                slow = head
                while slow != fast:
                    slow = slow.next
                    fast = fast.next
                return slow
        return None

Welcome to public concern number: Write Love Bug (ID: iCodeBugs)

I love to write bug.png

Guess you like

Origin www.cnblogs.com/zhangzhe532/p/11200194.html