天天看點

LeetCode 142:環形連結清單 II Linked List Cycle II

給定一個連結清單,傳回連結清單開始入環的第一個節點。 如果連結清單無環,則傳回

null

為了表示給定連結清單中的環,我們使用整數

pos

來表示連結清單尾連接配接到連結清單中的位置(索引從 0 開始)。 如果

pos

-1

,則在該連結清單中沒有環。

說明:不允許修改給定的連結清單。

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.

示例 1:

輸入:head = [3,2,0,-4], pos = 1
輸出:tail connects to node index 1
解釋:連結清單中有一個環,其尾部連接配接到第二個節點。           

示例 2:

輸入:head = [1,2], pos = 0
輸出:tail connects to node index 0
解釋:連結清單中有一個環,其尾部連接配接到第一個節點。           

示例 3:

輸入:head = [1], pos = -1
輸出:no cycle
解釋:連結清單中沒有環。           

進階: 你是否可以不用額外空間解決此題?

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

解題思路:

和上一道題比隻多了一步判斷入環節點在哪。兩種方法:

哈希表:

哈希表添加節點時隻要發現節點已經存在了,證明就有環形連結清單。并且已存在的節點即為入環節點

雙指針:

畫了個圖幫助了解:

LeetCode 142:環形連結清單 II Linked List Cycle II

一快一慢雙指針開始從頭結點周遊連結清單,快節點速度為2,慢節點速度為1:

相遇時:

慢節點走了:

a+b

由于快指針速度是慢指針的2倍,快節點走了:

2(a+b)

快慢節點相遇時快節點比慢節點剛好多走了一圈環形節點。快節點走了:

(a+b)+(b+c)

列方程:

2(a+b)=(a+b)+(b+c)

解得

a=c

也就是說:相遇節點到入環節點的長度和頭節點到入環節點的長度相等

可以得出結論,如果此時讓慢節點或快節點中的一個指向頭節點,另一個留在相遇節點,然後速度都為1,繼續周遊連結清單,雙指針再次相遇時的節點剛好是入環節點。

注:為了了解友善,把長度 b 定為上半部分長度,實際上 b 應該為快慢節點相遇時慢節點繞過環形連結清單的總長度

哈希表解題:

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           

雙指針解題:

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;
    }
}           
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           
歡迎關注公衆号:愛寫Bug(ID:iCodeBugs)

繼續閱讀