# How to detect loop in a linked list in java with example

If you want to practice data structure and algorithm programs, you can go through data structure and algorithm interview questions.

One of the most popular interview question nowadays is “How to detect loop/cycle in LinkedList”. So I thought I should cover this question. This question is more related to data structure. You can also find start node of loop if loop exists.

## Java Linked List Interview Programs:

First approach that you may think may something look like:

• Traverse through each node till end , tracking visited node using visited flag.
• If you find node that is already visited, then there is a loop in LinkedList and if you reach till end while traversing then there is no loop in LinkedList

But problem with above approach is, in most cases you can not change data structure of LinkedList node, so you won’t be able to add visited flag to it.

#### Efficient approach:

Efficient approach for this problem would be Floyd’s cycle detection algorithm,so steps for this algo would be:
• Use two pointer fastPtr and slowPtr and initialize both to head of linkedlist
• Move fastPtr by two nodes and slowPtr by one node in each iteration.
• If fastPtr and slowPtr meet at some iteration , then there is a loop in linkedlist.
• If fastPtr reaches to the end of linkedlist without meeting slow pointer then there is no loop in linkedlist (i.e fastPtr->next or fastPtr->next->next become null)

Java code for this algo will be
Above solution work with o(n) time complexity and o(1) space complexity.

#### Lets create linked list without loop :

Lets create a java program called “LinkedList.java”
Logically our linkedlist can be represented as :

Run above program, you will get following output:

#### Lets create a loop in linkedlist

Now we will connect last node to the nodewith value 7 and it will create loop in linked list, so change above main method to this:

Logically our linkedlist with loop can be represented as :

Run above program, you will get following output:

so this is how you can detect a loop in linkedlist.
Please go through java interview programs for more such programs.

import_contacts

## Related Posts

• 12 August

### Intersection of two linked lists

Table of ContentsProblemSolution If you want to practice data structure and algorithm programs, you can go through 100+ java coding interview questions. In this post, we will see how to find Intersection of two linked lists. Problem Given two singly linked lists, find if two linked lists intersect. If they intersect, find intersection point. Solution Here is […]

• 12 October

### Implement Queue using Linked List in java

If you want to practice data structure and algorithm programs, you can go through 100+ java coding interview questions. In this post , we will see how to implement Queue using Linked List in java. Queue is abstract data type which demonstrates First in first out (FIFO) behaviour. We will implement same behaviour using Array. Although java provides implementation […]

• 10 October

### Convert sorted Linked List to balanced BST

If you want to practice data structure and algorithm programs, you can go through 100+ java coding interview questions. In this post , we will see how to convert sorted LinkedList to balanced binary search tree. There are two ways to do it. Solution 1: It is very much similar to convert sorted array to BST. […]

• 20 September

### Find length of Linked List in java

If you want to practice data structure and algorithm programs, you can go through data structure and algorithm programs. In this post, we will see how to find length of Linked List in java. You can obviously use size() method of java Linked List class but here we are going to see how to find length […]

• 20 September

### Implement singly linked list in java

In this post, we will see how to implement singly linked list in java. It is one of the most used data structure. In singly linked list, Node has data and pointer to next node. It does not have pointer to the previous node. Last node ‘s next points to null, so you can iterate […]

• 10 September

### Implement stack using Linked List in java

If you want to practice data structure and algorithm programs, you can go through 100+ java coding interview questions. In this program, we will see how to implement stack using Linked List in java. The Stack is an abstract data type that demonstrates Last in first out (LIFO) behavior. We will implement the same behavior using […]

1. Narendra says:

2. Rajesh says: