LeetCode------Linked List Cycle
題目簡介
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
自己的解法
剛開始看了題目沒什麼思路,剛開始以為節點的val是固定的,後面發現不是。(不能預設成立阿)後來看了一下資料才明白思路。就是設定兩個節點一個快每次走兩個節點,一個慢一次走一個節點。如果是環的話,始終會遇到。public class Solution { public boolean hasCycle(ListNode head) { if (head == null) return false; if (head.next == null) return false; ListNode fast = head.next; ListNode slow = head; while (fast != slow){ if (fast.next == null || fast.next.next == null) return false; fast = fast.next.next; slow = slow.next; } return true; } }
Hot解法
Hot解法的思路與我們基本相同,我的寫法迴圈的條件是判斷快節點和慢節點是否相等,為了解決第一次的問題,我只能先讓fast = head.next(在此之前還要先判斷)。因此我的程式碼沒有那麼美觀,前面多了很多單獨的操作。但是如果把迴圈的條件換成public boolean hasCycle(ListNode head) { if(head==null) return false; ListNode walker = head; ListNode runner = head; while(runner.next!=null && runner.next.next!=null) { walker = walker.next; runner = runner.next.next; if(walker==runner) return true; } return false; }
runner.next!=null && runner.next.next!=null
就少了我前面的那些操作,使程式碼更美觀。
- 我們的解法更應該追求一致性,儘量都可以放到迴圈裡處理,而少一些需要單獨拿出來處理的操作。
相關推薦
[LeetCode] Linked List Cycle II
ack key init 實現 div cycle ctc word add Given a linked list, return the node where the cycle begins. If there is no cycle, retur
[LeetCode] Linked List Cycle
public {} red etc lin nbsp log 每次 使用 Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext
Leetcode:Linked List Cycle II
所有 public clas 因此 包含 表頭 class return node 題目大意:判斷一個鏈表是否含有環,如果有環則輸出距離鏈表頭最近的環上結點(即從鏈表頭出發進入環的入口)。 有趣的題目,一般判斷鏈表是否有環可以同時使用兩個軌跡結點遍歷整個鏈表,且軌跡結
leetcode Linked List Cycle
Linked List Cycle 題目:https://leetcode.com/problems/linked-list-cycle/ 判斷一個單鏈表有沒有環,不能使用額外的空間 解題思路:使用快慢指標,一個快指標每次走兩格,一個慢指標,每次走一格,如果快慢指標指標指向同一個
Leetcode linked-list-cycle 判斷連結串列是否有環
題目描述 Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 給定一個連結串列,判斷連結串列中否
LeetCode Linked List Cycle II
Problem Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note: Do not modi
[LeetCode] Linked List Cycle II 單鏈表中的環之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you solve it without using extra space?
[LeetCode] Linked List Cycle 單鏈表中的環
Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 這道題是快慢指標的經典應用。只需要設兩個指標,一個每次走一步的慢指標和一
[演算法][LeetCode]Linked List Cycle & Linked List Cycle II——單鏈表中的環
題目要求 Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 如何判斷一個單鏈表中有環? Li
LeetCode------Linked List Cycle
題目簡介 Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without
LeetCode | Linked List Cycle(判斷連結串列是否有環)
Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 題目解析: 判斷是否有環,只需要快慢指標即可。
LeetCode|Linked List Cycle II
題目 Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you solve it without u
LeetCode-Linked List Cycle
尋找連結串列是否有環 方法是快慢指標 第一次相遇時slow走過的距離:a+b,fast走過的距離:a+b+c+b。 因為fast的速度是slow的兩倍,所以fast走的距離是slow的兩倍,有 2(a+b) = a+b+c+b,可以得到a=c slo
Leetcode: Linked List Cycle II
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x),
leetcode鏈表--6、linked-list-cycle-ii(有環單鏈表環的入口結點)
pre you head lis 頭結點 tex -a init int 題目描述 Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull
LeetCode 142. Linked List Cycle II
linked blank code margin following ucs follow etc ref c臣8賦oe棧綠4敲ghttp://www.facebolw.com/space/2105094/following 擻r2灸6u186嫉雲磷俳8http://ww
[Leetcode]141. Linked List Cycle
bool pan turn code asc lin cnblogs solution false Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it with
LeetCode 141, 142. Linked List Cycle I+II
有環 TP clas 起點 urn 沒有 nbsp 快慢指針 list 判斷鏈表有沒有環,用Floyd Cycle Detection算法,用兩個快慢指針。 class Solution { public: bool hasCycle(ListNode *
[leetcode]141. Linked List Cycle判斷鏈表是否有環
code AC LV class you return In CA .com Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using e
leetcode-142 Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note: Do not modify the linked list. 想法:(1)首先的判斷連結串列中是否有環,若