欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
0142. Linked List Cycle II (M)

Linked List Cycle II (M)

題目

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:

Input: head = [3,2,0,-4], pos = 1
Output: tail connects to node index 1
Explanation: There is a cycle in the linked list, where tail connects to the second node.

Example 2:

Input: head = [1,2], pos = 0
Output: tail connects to node index 0
Explanation: There is a cycle in the linked list, where tail connects to the first node.

Example 3:

Input: head = [1], pos = -1
Output: no cycle
Explanation: There is no cycle in the linked list.

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


題意

判斷給定鏈表中是否存在環(huán),存在則返回環(huán)的入口結點(diǎn)。

思路

比較簡(jiǎn)單的就是將所有遍歷到的結點(diǎn)記錄下來(lái),如果記錄了兩次則說(shuō)明當前結點(diǎn)就是所求的結點(diǎn)。

同樣可以使用快慢指針的方法:慢指針每次走一步,快指針每次走兩步,如果快指針追上慢指針則說(shuō)明存在環(huán);當判斷出存在環(huán)后,將快指針重新指向頭結點(diǎn),步進(jìn)距離改為一個(gè)結點(diǎn),然后使快指針和慢指針同時(shí)繼續前進(jìn),當兩者再次相遇時(shí),所處結點(diǎn)就是所求入口結點(diǎn)。證明如下:

記第一次相遇時(shí)慢指針走過(guò)的距離為\(S_1\),快指針走過(guò)的距離為\(S_2\),那么可得如下方程組:

\[\begin{cases} S_1=x+y \S_2=x+y+n*(y+z) \S_2=2*S_1 \end{cases} \]

化簡(jiǎn)后可得:

\[x=(n-1)*(y+z)+z \]

說(shuō)明當快指針重新從A點(diǎn)走到B點(diǎn)時(shí),慢指針從C點(diǎn)出發(fā)已經(jīng)走過(guò)了n圈加上z的距離,即也正好落在B點(diǎn)上,因此上述方法能夠正確找到環(huán)的入口結點(diǎn)。


代碼實(shí)現

Java

Hash

public class Solution {
    public ListNode detectCycle(ListNode head) {
        if (head == null) {
            return null;
        }

        Set<ListNode> set = new HashSet<>();
        while (head != null) {
            if (!set.add(head)) {
                return head;
            }
            head = head.next;
        }

        return null;
    }
}

快慢指針

public class Solution {
    public ListNode detectCycle(ListNode head) {
        if (head == null) {
            return null;
        }

        ListNode slow = head, fast = head;
        while (fast.next != null && fast.next.next != null) {
            slow = slow.next;
            fast = fast.next.next;
            if (slow == fast) {
                fast = head;
                while (slow != fast) {
                    slow = slow.next;
                    fast = fast.next;
                }
                return slow;
            }
        }

        return null;
    }
}

JavaScript

/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var detectCycle = function (head) {
  let slow = head
  let fast = head

  while (fast && fast.next) {
    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
}
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
141  Linked List Cycle
141. Linked List Cycle (E)
460. 快慢指針解環(huán)形鏈表 II
判斷鏈表是否有環(huán)
程序員編程藝術(shù):第九章、閑話(huà)鏈表追趕問(wèn)題
劍指offer(C++)-JZ23:鏈表中環(huán)的入口結點(diǎn)(數據結構-鏈表)
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久