141/142. Linked List Cycle I/II (Easy/Medium)

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 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.


141/142. Linked List Cycle I/II (Easy/Medium)_第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.


141/142. Linked List Cycle I/II (Easy/Medium)_第2张图片

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?


Solution:

# 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
        """
        dic = {}
        curr = head
        i = 0
        while curr != None:
            if curr not in dic:
                dic[curr] = I
                curr = curr.next
                i += 1
            else:
                return True
                
        return False

Runtime: 32 ms, faster than 94.98% of Python online submissions for Linked List Cycle.
Memory Usage: 19 MB, less than 7.41% of Python online submissions for Linked List Cycle.

O(1) space solution: inspired by https://www.cnblogs.com/grandyang/p/4137187.html

Grandyang:

这道题是快慢指针的经典应用。只需要设两个指针,一个每次走一步的慢指针和一个每次走两步的快指针,如果链表里有环的话,两个指针最终肯定会相遇。

class Solution(object):
    def hasCycle(self,head):
        """
        :type head: ListNode
        :rtype: bool
        """
        slow = head
        fast = head
        while slow != None and fast != None and fast.next != None:
            slow = slow.next
            fast = fast.next.next
            if slow == fast:
                return True
        return False

Runtime: 40 ms, faster than 66.67% of Python online submissions for Linked List Cycle.
Memory Usage: 18.2 MB, less than 33.80% of Python online submissions for Linked List Cycle.


Given a linked list, return the node where the cycle begins. If there is no cycle, return null.

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.

Note: Do not modify the linked list.

Follow-up:
Can you solve it without using extra space?


Solutions:

class Solution(object):
    def detectCycle(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        dic = {}
        i = 0
        curr = head
        while curr != None:
            if curr not in dic:
                dic[curr] = i
                i += 1
                curr = curr.next
            else:
                return curr
        return None

Runtime: 28 ms, faster than 99.51% of Python online submissions for Linked List Cycle II.
Memory Usage: 19.1 MB, less than 5.24% of Python online submissions for Linked List Cycle II.

O(1) space solution: inspired by https://www.cnblogs.com/grandyang/p/4137302.html

class Solution(object):
    def detectCycle(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """

        slow = head
        fast = head
        while slow != None and fast != None and fast.next != None:
            slow = slow.next
            fast = fast.next.next
            if fast == slow:
                break
                
        if fast == None or fast.next == None:
            return None
        
        fast = head
        while slow != fast:
            slow = slow.next
            fast = fast.next
        return slow

Runtime: 52 ms, faster than 15.51% of Python online submissions for Linked List Cycle II.
Memory Usage: 18.2 MB, less than 56.01% of Python online submissions for Linked List Cycle II.

你可能感兴趣的:(141/142. Linked List Cycle I/II (Easy/Medium))