Day 5: Fast and Slow pointers (Cycle detection)

Day 5: Fast and Slow pointers (Cycle detection)


Linked list cycle


Linked List Cycle

Linked List Tutorial

Recommended: Solution

Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).

def hasCycle(head):
        # If the list is empty, there is no cycle
        if not head:
            return False

        # Initialize two pointers, fast and slow
        # Fast moves 2 steps at a time and slow moves 1 step at a time
        fast = head.next
        slow = head.next

        # Loop until the fast pointer reaches the end of the list
        while fast and fast.next:
            # Move slow pointer by 1 step
            slow = slow.next
            # Move fast pointer by 2 steps
            fast = fast.next.next

            # If slow and fast meet, there's a cycle in the list
            if slow == fast:
                return True

        # If we exit the loop, fast reached the end of the list, hence no cycle
        return False        


Time Complexity = O(n)

Space Complexity = O(1)

If you have any questions, put them in the comment section. I will be glad to answer them!

Victor Chiedo Ogbonna

Tech Innovator: Digital Marketing Specialist | Full-Stack Web Developer | Python & AI Researcher | Power BI Analyst

8 个月

Good to know!

要查看或添加评论,请登录

Olaoluwa Saola的更多文章

  • Amazon Redshift Data Warehouse Project Using S3, Boto3, and psycopg2

    Amazon Redshift Data Warehouse Project Using S3, Boto3, and psycopg2

    In this project, I worked with Amazon Redshift and S3 to build a data warehouse solution, automating data ingestion and…

    2 条评论
  • Data Warehouse part 2: OLAP vs OLTP

    Data Warehouse part 2: OLAP vs OLTP

    Data engineering plays a pivotal role in modern data management, enabling organizations to efficiently process and…

    2 条评论
  • Building a Data Warehouse Part 1: A Step-by-Step Guide

    Building a Data Warehouse Part 1: A Step-by-Step Guide

    In today’s data-driven world, the ability to effectively store, manage, and analyze large volumes of data is crucial…

    8 条评论
  • Data Engineering 101: Setting Up PostgreSQL with Python

    Data Engineering 101: Setting Up PostgreSQL with Python

    Read article on Medium Data engineering involves managing and processing data for analysis, which often starts with…

    4 条评论
  • Day 20: Find Median of a Datastream (Two Heaps)

    Day 20: Find Median of a Datastream (Two Heaps)

    Median of a Datastream Recommended: Solution The median is the middle value in an ordered integer list. If the size of…

  • Day 19: Modified Binary Search (Search Insert Position)

    Day 19: Modified Binary Search (Search Insert Position)

    Search Insert Position Recommended: Solution Given a sorted array of distinct integers and a target value, return the…

  • Day 18 Missing Number (Cyclic Sort)

    Day 18 Missing Number (Cyclic Sort)

    Missing Number Recommended: Solution Given an array containing distinct numbers in the range , return the only number…

    2 条评论
  • Day 17: Path Sum II (DFS)

    Day 17: Path Sum II (DFS)

    Path Sum II Recommended: Solution Given the of a binary tree and an integer , return all root-to-leaf paths where the…

  • Day 16: Path Sum (Tree Depth First Search)

    Day 16: Path Sum (Tree Depth First Search)

    Path Sum Recommended: Solution Given the of a binary tree and an integer , return if the tree has a root-to-leaf path…

    1 条评论
  • Recap

    Recap

    ?? Here are the links to my previous posts: Day 15: Zig zag Binary Tree Traversal Day 14: Reverse Level order traversal…

    4 条评论

社区洞察

其他会员也浏览了