141. Linked List Cycle

141. Linked List Cycle

Please refer to LeetCode for the question details:

https://leetcode.com/problems/linked-list-cycle/description/

We are asked check if a Linked List contains a cycle.

Intuition

Slow and fast pointers pattern is used to find cycle in a linked list

Approach

  1. Initialize slow and fast pointer with the head of the linkedList
  2. Move slow pointer by one step, and fast pointer by two steps.
  3. Repeat step 3 until one of the following conditions are met: i. fast is null or fast.next is null. No Cycle ii. fast is equal to slow. Found cycle

Complexity

  • Time complexity: O(n)
  • Space complexity: O(1)

Code

Here are the performance metrics:

Please consider upvoting my solution if you found it helpful!

https://leetcode.com/problems/linked-list-cycle/solutions/4830189/interview-solution-time-on-space-o1-beats-9655

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

Sajid Khan的更多文章

  • Minimum Length of String After Deleting Similar Ends

    Minimum Length of String After Deleting Similar Ends

    Please refer to LeetCode for the question details:…

  • Bag of Tokens

    Bag of Tokens

    Please refer to LeetCode for the question descriptions. https://leetcode.

  • Maximum Odd Binary Number

    Maximum Odd Binary Number

    Of course, the month begins with a simple question, but it's important to attempt every challenge. In this question, we…

  • Even Odd Tree

    Even Odd Tree

    https://leetcode.com/problems/even-odd-tree/description/?envType=daily-question&envId=2024-02-29 If you look into the…

    1 条评论

社区洞察

其他会员也浏览了