LeetCode141. ็Žฏๅฝข้“พ่กจ๐ŸŒŸ๐ŸŒŸ๐ŸŒŸ๐ŸŒŸ๐ŸŒŸ็ฎ€ๅ•

่ฏพๅŽไฝœไธš

้—ฎ้ข˜ๆ่ฟฐ

ๅŽŸๆ–‡้“พๆŽฅ๏ผš141. ็Žฏๅฝข้“พ่กจ

็ป™ไฝ ไธ€ไธช้“พ่กจ็š„ๅคด่Š‚็‚น head ๏ผŒๅˆคๆ–ญ้“พ่กจไธญๆ˜ฏๅฆๆœ‰็Žฏใ€‚

ๅฆ‚ๆžœ้“พ่กจไธญๆœ‰ๆŸไธช่Š‚็‚น๏ผŒๅฏไปฅ้€š่ฟ‡่ฟž็ปญ่ทŸ่ธช next ๆŒ‡้’ˆๅ†ๆฌกๅˆฐ่พพ๏ผŒๅˆ™้“พ่กจไธญๅญ˜ๅœจ็Žฏใ€‚ ไธบไบ†่กจ็คบ็ป™ๅฎš้“พ่กจไธญ็š„็Žฏ๏ผŒ่ฏ„ๆต‹็ณป็ปŸๅ†…้ƒจไฝฟ็”จๆ•ดๆ•ฐ pos ๆฅ่กจ็คบ้“พ่กจๅฐพ่ฟžๆŽฅๅˆฐ้“พ่กจไธญ็š„ไฝ็ฝฎ๏ผˆ็ดขๅผ•ไปŽ 0 ๅผ€ๅง‹๏ผ‰ใ€‚ๆณจๆ„๏ผšpos ไธไฝœไธบๅ‚ๆ•ฐ่ฟ›่กŒไผ ้€’ ใ€‚ไป…ไป…ๆ˜ฏไธบไบ†ๆ ‡่ฏ†้“พ่กจ็š„ๅฎž้™…ๆƒ…ๅ†ตใ€‚

ๅฆ‚ๆžœ้“พ่กจไธญๅญ˜ๅœจ็Žฏ ๏ผŒๅˆ™่ฟ”ๅ›ž true ใ€‚ ๅฆๅˆ™๏ผŒ่ฟ”ๅ›ž false ใ€‚

็คบไพ‹ 1๏ผš

img

่พ“ๅ…ฅ๏ผšhead = [3,2,0,-4], pos = 1
่พ“ๅ‡บ๏ผštrue
่งฃ้‡Š๏ผš้“พ่กจไธญๆœ‰ไธ€ไธช็Žฏ๏ผŒๅ…ถๅฐพ้ƒจ่ฟžๆŽฅๅˆฐ็ฌฌไบŒไธช่Š‚็‚นใ€‚

็คบไพ‹ 2๏ผš

img

่พ“ๅ…ฅ๏ผšhead = [1,2], pos = 0
่พ“ๅ‡บ๏ผštrue
่งฃ้‡Š๏ผš้“พ่กจไธญๆœ‰ไธ€ไธช็Žฏ๏ผŒๅ…ถๅฐพ้ƒจ่ฟžๆŽฅๅˆฐ็ฌฌไธ€ไธช่Š‚็‚นใ€‚

็คบไพ‹ 3๏ผš

img

่พ“ๅ…ฅ๏ผšhead = [1], pos = -1
่พ“ๅ‡บ๏ผšfalse
่งฃ้‡Š๏ผš้“พ่กจไธญๆฒกๆœ‰็Žฏใ€‚

ๆ็คบ๏ผš

  • ้“พ่กจไธญ่Š‚็‚น็š„ๆ•ฐ็›ฎ่Œƒๅ›ดๆ˜ฏ [0, 104]
  • -105 <= Node.val <= 105
  • pos ไธบ -1 ๆˆ–่€…้“พ่กจไธญ็š„ไธ€ไธชๆœ‰ๆ•ˆ็ดขๅผ• ใ€‚

ไปฃ็ ๅฎž็Žฐ

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) {
        if(head == null){
            return false;
        }
        ListNode fast = head;
        ListNode 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):
        """
        :type head: ListNode
        :rtype: bool
        """
        if not head:
            return False

        fast = head
        slow = head

        while fast and fast.next:
            fast = fast.next.next
            slow = slow.next

            if fast == slow:
                return True

        return False

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) {
        if(!head){
            return false;
        }
        ListNode* fast = head;
        ListNode* slow = head;

        while(fast && fast->next){
            fast = fast->next->next;
            slow = slow->next;
            if(fast == slow){
                return true;
            }
        }
        return false;
    }
};

Go

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func hasCycle(head *ListNode) bool {
    if head == nil {
        return false
    }

    fast := head
    slow := head

    for fast != nil && fast.Next != nil {
        fast = fast.Next.Next
        slow = slow.Next

        if fast == slow {
            return true
        }
    }

    return false
}

ๅ‘่กจ่ฏ„่ฎบ

ๅŽๆ‰่ƒฝ่ฏ„่ฎบ