Creating cycles from set of integers

Given a set of integers can we form a linked list with cycle ? How so?

Let’s say you are given: 1 2 3 4 5 2 3, how do you keep track as to where to form the node, and if there are multiple cycles.

In the given case, the possibilities can be:

1->2->3->4->5->2->(3->4->)…
and 1->2->3->4->5->(2->3->4->)…

Kindly help;
Further kindly share code;

Hey Aditya, if any element in the list of integers is repeating that means it is a cycle there.
for eg. 1 2 3 4 5 2 in this we have a cycle 2->3->4->5->2
we can keep track of it if there is a loop exist or not by checking if any element which we are inserting already exists in the list.

1 Like