Leetcode 141. Linked List Cycle


這題有兩種解法:Hash Table 和 Two pointers,兩個的Time complexity 都是O(1),但是空間複雜度大不同,Hash Table是 O(n),Two pointers是O(1)。

Hash Table的解法是將沒有看過的,insert進去ListNode*,接著下一次出現就return true,每一次都將head =  head->next。

Two pointers的解法是,有兩個跑者一個每次跑一步,一個每次跑兩布,如我有cycle,他們到最後會匯合。這邊要注意的是while的判斷式,我在這邊卡很久,要以跑兩步的跑者做為基準。


Hash Table 解法

Two Pointers 解法

留言

  1. Matched Betting by Online Bookmakers | The Best Matched Betting Sites
    Today, it's important to understand how matchpoint to understand matched betting using 샌즈카지노 matched betting sites, even for new and old bettingtors. It can also 더킹카지노 be used

    回覆刪除

張貼留言

這個網誌中的熱門文章

面試 (網路搜尋的資源)

bitwise operation 面試考題