LeetCode 141. Linked List Cycle Solution Example (Python)

LeetCode question 141. Here is an example of the answer to Linked List Cycle in Python. https://leetcode.com/problems/linked-list-cycle/

Problem overview

The problem statement is simple: Given a linked list, determine if it has a cycle in it. When a linked list is given, it has a cycle in it. The argument is a linked list.

How to solve

The algorithm for solving is simple. Here, let's consider Example 1 of the problem statement as an example. First, the graph in question (it's called a graph here because it resembles a graph as a data structure, not a graph up to high school mathematics).

IMG_663C064CD19D-1.jpeg

Now consider slow pointer and fast pointer. The starting point here is the leftmost 3. The slow pointer advances one at a time, and the fast pointer advances two at a time. Now, if you proceed once here, it will look like the figure below.

IMG_BC0790961F0D-1.jpeg

After proceeding twice, it will be as shown in the figure below.

IMG_BC0790961F0D-1.jpeg

After proceeding 3 times, it will be as shown in the figure below.

IMG_B423B6363F33-1.jpeg

After going three times, the slow pointer and fast pointer came to the same location (node 4).

Answer example

Below are two examples of answers.

Answer example 1

class Solution:
    def hasCycle(self, head: ListNode) -> bool:
        """
        type head: ListNode
        return type: bool
        """

        slow = fast = head
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
            if slow == fast:
                return True
        return False

The execution result of answer example 1 is as follows. At the time of this run, it seems to be 64.54% faster than the average submission in Python3. Similarly, memory usage seems to be 100% less than that.

Screen Shot 2020-03-01 at 22.59.53.png

Answer example 2

class Solution:
    def hasCycle(self, head: ListNode) -> bool:
        """
        type head: ListNode
        return type: bool
        """

        try:
            slow = head
            fast = head.next
            while slow != fast:
                slow = slow.next
                fast = fast.next.next
                
            return True
        except:
            return False

The execution result of answer example 2 is as follows. At the time of this run, it seems to be 33.8% faster than the average submission in Python3. Similarly, memory usage seems to be 100% less than that. The reason why the processing speed is slower than the answer example 1 is thought to be that try except performs the judgment processing every time and the if statement is increased by one, so it is slower by that amount.

Screen Shot 2020-03-02 at 11.56.27.png

Recommended Posts

LeetCode 141. Linked List Cycle Solution Example (Python)
I tried LeetCode every day 141. Linked List Cycle (Python, Go)
linked list
[Python] list
Python basics: list
[Python] Solution to the problem that elements are linked when copying a list
Python> Comprehension / Comprehension> List comprehension
Python list manipulation
LeetCode Python [updating]
Let Code Day 22 starting from scratch "141. Linked List Cycle"
Sorted list in Python
Python Exercise 2 --List Comprehension
List of python modules
Python> list> extend () or + =
Python list comprehension speed
Filter List in Python
python unittest assertXXX list
Python3 List / dictionary memo
[Memo] Python3 list sort
OpenCV3 Python API list
Python error list (Japanese)
List find in Python
Python exception class list
Initialize list with python
Let Code Day 84 starting from zero "142. Linked List Cycle II"