Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

70. Linked List Cycle

EasyDay 21 of 60Linked List

Given the head of a linked list, return true if the list contains a cycle, meaning some node can be reached again by following next pointers. In the tests, pos is the index of the node the tail links back to (-1 means no cycle); pos is only used to build the list and is not passed to your function.

Example 1
Input: head = [3,2,0,-4], pos = 1
Output: true
The tail (-4) points back to the node with value 2.
Example 2
Input: head = [1], pos = -1
Output: false
Constraints
  • The list has 0 to 10^4 nodes
  • -10^5 <= Node.val <= 10^5
  • `pos` is -1 or a valid index
Hints
Frequently asked at: Amazon, Microsoft, Bloomberg, Wipro, TCS
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min