[LeetCode]#141. Linked List Cycle

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.
Input: head = [1,2], pos = 0
Output: true
Explanation: There is a cycle in the linked list, where tail connects to the first node.
Input: head = [1], pos = -1
Output: false
Explanation: There is no cycle in the linked list.
class Solution:
def hasCycle(self, head: ListNode) -> bool:
slow = head
fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast: return True
return False

Interesting in any computer science.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Axios Throttle

TASK 23

Log Postgres Running Queries -Docker

The Best Front-End Hacking Cheatsheets — All in One Place

Let’s Make a Loot System in Unity!

Back to the Basics: Recursion

DateOnly and TimeOnly with HttpRequest in .NET 6

OpenFaaS on DCOS

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Fatboy Slim

Fatboy Slim

Interesting in any computer science.

More from Medium

[LeetCode]#2057. Smallest Index With Equal Value

How to implement a Queue class using deque. PYTHON data structures and algorithm.

Use of python dictionary get function

Remove nth node from end of list Leetcode Python