Linked List Cycle: solved

Anastasia Orlova
Jan 18 · 2 min read
Image for post
Image for post

Dashing through Leetcode problems, I found this one: Linked List Cycle. In this blog, I’ll solve it using the two-pointer approach.

Our task is to determine if the linked list has a cycle in it. Return true if there is a cycle in the linked list. Otherwise, return false.

What is considered to be a cycle? There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Have a look at this example.

Image for post
Image for post
leetcode.com

Input: head = [3,2,0,-4], pos = 1 (the index of the node that tail’s next pointer is connected to; not passed as a parameter)
Output: true
Explanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).

Here is my approach which I found the most intuitive. It is called a two-pointer approach with slow and fast pointers.

So, our first step is to create two pointers. The first one is slow because it moves only one node from the previous position. The second one is fast — it moves two nodes from the previous position.

Then we will create a while loop and at each iteration check:

  • whether the fast pointer reached the end. If yes, return false because it’s not a cycle.
  • whether both fast and slow pointers are at the same node. If yes, there is a cycle because without a cycle it’s impossible to come to the same position.

So, my code is:

var hasCycle = function(head) {    if(head === null) return false; //if there is no head node, there is no cycle    let slow = head;
let fast = head.next;
while(slow !== fast && fast !== null) {
if(fast.next === null) return false
fast = fast.next.next
slow = slow.next
}
return slow === fast ? true : false
};

Hope that was helpful!

Sign up for Best Stories

By Dev Genius

The best stories sent monthly to your email. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Anastasia Orlova

Written by

Full-Stack Developer with a background in retail and social work // anastasia-orlova.netlify.app

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Anastasia Orlova

Written by

Full-Stack Developer with a background in retail and social work // anastasia-orlova.netlify.app

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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