LeetCode #141 Linked List Cycle 環(huán)形鏈表

141 Linked List Cycle 環(huán)形鏈表

Description:
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:

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 1

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 2

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

Example 3

Follow up:

Can you solve it using O(1) (i.e. constant) memory?

題目描述:
給定一個(gè)鏈表沽讹,判斷鏈表中是否有環(huán)。

為了表示給定鏈表中的環(huán)琅拌,我們使用整數(shù) pos 來表示鏈表尾連接到鏈表中的位置(索引從 0 開始)惨缆。 如果 pos-1绘搞,則在該鏈表中沒有環(huán)。

示例:

示例 1:
輸入:
head = [3,2,0,-4], pos = 1
輸出:
true
解釋:
鏈表中有一個(gè)環(huán),其尾部連接到第二個(gè)節(jié)點(diǎn)挽荡。

示例 1

示例 2:
輸入:
head = [1,2], pos = 0
輸出:
true
解釋:
鏈表中有一個(gè)環(huán)倦零,其尾部連接到第一個(gè)節(jié)點(diǎn)误续。

示例 2

示例 3:
輸入:
head = [1], pos = -1
輸出:
false
解釋:
鏈表中沒有環(huán)。

示例 3

進(jìn)階:
你能用 O(1)(即扫茅,常量)內(nèi)存解決此問題嗎蹋嵌?

思路:

快慢指針
時(shí)間復(fù)雜度O(n), 空間復(fù)雜度O(1)

代碼:
C++:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution 
{
public:
    bool hasCycle(ListNode *head) 
    {
        ListNode *fast = head, *slow = head;
        while (fast and fast -> next) 
        {            
            fast = fast -> next -> next;
            slow = slow -> next;
            if (fast == slow) return true;
        }
        return false;
    }
};

Java:

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public boolean hasCycle(ListNode head) {
        ListNode fast = head, slow = head;
        while (fast != null && fast.next != null) {            
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) return true;
        }
        return false;
    }
}

Python:

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None
class Solution(object):
    def hasCycle(self, head: ListNode) -> ListNode:
        slow = fast = head
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
            if slow == fast:
                return True
        return False
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市葫隙,隨后出現(xiàn)的幾起案子栽烂,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,036評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件愕鼓,死亡現(xiàn)場離奇詭異钙态,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)菇晃,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,046評論 3 395
  • 文/潘曉璐 我一進(jìn)店門册倒,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人磺送,你說我怎么就攤上這事驻子。” “怎么了估灿?”我有些...
    開封第一講書人閱讀 164,411評論 0 354
  • 文/不壞的土叔 我叫張陵崇呵,是天一觀的道長。 經(jīng)常有香客問我馅袁,道長域慷,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,622評論 1 293
  • 正文 為了忘掉前任汗销,我火速辦了婚禮犹褒,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘弛针。我一直安慰自己叠骑,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,661評論 6 392
  • 文/花漫 我一把揭開白布削茁。 她就那樣靜靜地躺著宙枷,像睡著了一般。 火紅的嫁衣襯著肌膚如雪茧跋。 梳的紋絲不亂的頭發(fā)上慰丛,一...
    開封第一講書人閱讀 51,521評論 1 304
  • 那天,我揣著相機(jī)與錄音瘾杭,去河邊找鬼璧帝。 笑死,一個(gè)胖子當(dāng)著我的面吹牛富寿,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播锣夹,決...
    沈念sama閱讀 40,288評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼页徐,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了银萍?” 一聲冷哼從身側(cè)響起变勇,我...
    開封第一講書人閱讀 39,200評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后搀绣,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體飞袋,經(jīng)...
    沈念sama閱讀 45,644評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,837評論 3 336
  • 正文 我和宋清朗相戀三年链患,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了巧鸭。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,953評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡麻捻,死狀恐怖纲仍,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情贸毕,我是刑警寧澤郑叠,帶...
    沈念sama閱讀 35,673評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站明棍,受9級特大地震影響乡革,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜摊腋,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,281評論 3 329
  • 文/蒙蒙 一沸版、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧歌豺,春花似錦推穷、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,889評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至痕惋,卻和暖如春区宇,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背值戳。 一陣腳步聲響...
    開封第一講書人閱讀 33,011評論 1 269
  • 我被黑心中介騙來泰國打工议谷, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人堕虹。 一個(gè)月前我還...
    沈念sama閱讀 48,119評論 3 370
  • 正文 我出身青樓卧晓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親赴捞。 傳聞我的和親對象是個(gè)殘疾皇子逼裆,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,901評論 2 355

推薦閱讀更多精彩內(nèi)容