141. Linked List Cycle I

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).
Input: head = [1,2], pos = 0
Output: true
Explanation: There is a cycle in the linked list, where the tail connects to the 0th node.
Input: head = [1], pos = -1
Output: false
Explanation: There is no cycle in the linked list.
  • The number of the nodes in the list is in the range [0, 104].
  • -105 <= Node.val <= 105
  • pos is -1 or a valid index in the linked-list.
def detectCycle(self, head):
temp=head
hash_set=set()
index=0
while(temp!=None):
if(temp.next in hash_set):
return True
else:
hash_set.add(temp)
temp=temp.next
return False
def hasCycle(self, head):
cnt=0
temp=head
while(temp!=None):
cnt+=1
if cnt>10**4:
return True

temp=temp.next
return False

--

--

--

Tech-Enthusiast, Coder,Explorer,Geeky,Software Engineer |A piece of code delivers everything that you need. The world is all about codes.

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

Recommended from Medium

Red Flags That Show Your Project Needs a Test Plan and a Test Strategy

Automate Compilation and Testing of Rust Code using GitHub Actions. (Part -2)

AWS Projects Ideas

Creating a Load Balancer using Apache HTTP server

Supply Chain Management Software Solutions

Chaos Engineering Case Study : HomeAway

Ready for Implementation, Done for Deployment

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
Dhanarajappu

Dhanarajappu

Tech-Enthusiast, Coder,Explorer,Geeky,Software Engineer |A piece of code delivers everything that you need. The world is all about codes.

More from Medium

Big O Notation - 1

Leetcode Easy: Binary Tree Inorder Traversal

Binary tree with a root of one. No left node, right node of 2.Node 2 has a left node of 3

Joy of Computer Program's Logic

Cracking the Coding Interview Book Problem Series: Problem 7