摘要:小鹿題目算法思路兩種解題思路哈希表法遍歷鏈表,沒遍歷一個(gè)節(jié)點(diǎn)就要在哈希表中判斷是否存在該結(jié)點(diǎn),如果存在,則為環(huán)否則,將該結(jié)點(diǎn)插入到哈希表中繼續(xù)遍歷。
Time:2019/4/7
Title: Linked List Cycle
Difficulty: Easy
Given a linked list, determine if it has a cycle in it.
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.
Example 1:
Input: head = [3,2,0,-4], pos = 1 Output: true 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: true Explanation: There is a cycle in the linked list, where tail connects to the first node.
Example 3:
Input: head = [1], pos = -1 Output: false Explanation: There is no cycle in the linked list.
Follow up:
Can you solve it using O(1) (i.e. constant) memory?
Slove:兩種解題思路:1)哈希表法:遍歷鏈表,沒遍歷一個(gè)節(jié)點(diǎn)就要在哈希表中判斷是否存在該結(jié)點(diǎn),如果存在,則為環(huán);否則,將該結(jié)點(diǎn)插入到哈希表中繼續(xù)遍歷。
2)用兩個(gè)快慢指針,快指針走兩步,慢指針走一步,如果快指針與慢指針重合了,則檢測(cè)的當(dāng)前鏈表為環(huán);如果當(dāng)前指針或下一指針為 null ,則鏈表不為環(huán)。
/** * Definition for singly-linked list. * function ListNode(val) { * this.val = val; * this.next = null; * } */ /** * @param {ListNode} head * @return {boolean} */ var hasCycle = function(head) { let fast = head; let map = new Map(); while(fast !== null){ if(map.has(fast)){ return true; }else{ map.set(fast); fast = fast.next; } } return false; };
var hasCycle = function(head) { if(head == null || head.next == null){ return false; } let fast = head.next; let slow = head; while(slow != fast){ if(fast == null || fast.next == null){ return false; } slow = slow.next; fast = fast.next.next; } return true; };
這部分代碼是我自己寫的,和上邊的快慢指針?biāo)悸废嗤?,運(yùn)行結(jié)果相同,但是當(dāng)運(yùn)行在 leetcode 時(shí),就會(huì)提示超出時(shí)間限制,仔細(xì)對(duì)比代碼,我們可以發(fā)現(xiàn),在邏輯順序上還是存在差別的,之所以超出時(shí)間限制,是因?yàn)榇a的運(yùn)行耗時(shí)長(zhǎng)。
//超出時(shí)間限制 var hasCycle = function(head) { if(head == null || head.next == null){ return false; } let fast = head.next; let slow = head; while(fast !== null && fast.next !== null){ if(slow === fast) return true; slow = head.next; fast = fast.next.next; } return false; };
歡迎一起加入到 LeetCode 開源 Github 倉(cāng)庫(kù),可以向 me 提交您其他語(yǔ)言的代碼。在倉(cāng)庫(kù)上堅(jiān)持和小伙伴們一起打卡,共同完善我們的開源小倉(cāng)庫(kù)!
Github:https://github.com/luxiangqia...
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/103335.html
摘要:說(shuō)明不允許修改給定的鏈表。算法思路題目要求返回單鏈表中存在循環(huán)鏈表的位置。首先,先判斷該單鏈表是否存在循環(huán)鏈表用兩個(gè)快慢指針?lè)謩e指向鏈表的頭部,每次移動(dòng)兩步,每次移動(dòng)一步,移動(dòng)的步數(shù)是的兩倍。 Time:2019/4/8Title: Linked List Cycle IIDifficulty: mediumAuthor:小鹿 題目:Linked List Cycle II Giv...
摘要:月下半旬攻略道題,目前已攻略題。目前簡(jiǎn)單難度攻略已經(jīng)到題,所以后面會(huì)調(diào)整自己,在刷算法與數(shù)據(jù)結(jié)構(gòu)的同時(shí),攻略中等難度的題目。 Create by jsliang on 2019-07-30 16:15:37 Recently revised in 2019-07-30 17:04:20 7 月下半旬攻略 45 道題,目前已攻略 100 題。 一 目錄 不折騰的前端,和咸魚有什么區(qū)別...
摘要:微信公眾號(hào)記錄截圖記錄截圖目前關(guān)于這塊算法與數(shù)據(jù)結(jié)構(gòu)的安排前。已攻略返回目錄目前已攻略篇文章。會(huì)根據(jù)題解以及留言內(nèi)容,進(jìn)行補(bǔ)充,并添加上提供題解的小伙伴的昵稱和地址。本許可協(xié)議授權(quán)之外的使用權(quán)限可以從處獲得。 Create by jsliang on 2019-07-15 11:54:45 Recently revised in 2019-07-15 15:25:25 一 目錄 不...
摘要:給定一個(gè)鏈表,判斷鏈表中是否有環(huán)。示例輸入輸出解釋鏈表中有一個(gè)環(huán),其尾部連接到第一個(gè)節(jié)點(diǎn)。哈希表解決重復(fù)問(wèn)題最容易想到的數(shù)據(jù)結(jié)構(gòu)就是哈希表,哈希表添加節(jié)點(diǎn)時(shí)只要發(fā)現(xiàn)節(jié)點(diǎn)已經(jīng)存在了,證明就有環(huán)形鏈表。 給定一個(gè)鏈表,判斷鏈表中是否有環(huán)。 為了表示給定鏈表中的環(huán),我們使用整數(shù) pos 來(lái)表示鏈表尾連接到鏈表中的位置(索引從 0 開始)。 如果 pos 是 -1,則在該鏈表中沒有環(huán)。 Giv...
閱讀 691·2021-11-23 09:51
閱讀 3434·2021-10-11 10:58
閱讀 15672·2021-09-29 09:47
閱讀 3624·2021-09-01 11:42
閱讀 1327·2019-08-29 16:43
閱讀 1859·2019-08-29 15:37
閱讀 2150·2019-08-29 12:56
閱讀 1762·2019-08-28 18:21