Cycle detection problem

Please tell me aobut Input , Output method for Cycle detection problem

Hi, please check the following testcase for more details.

Input-

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 48 

Output-

Cycle was present
Linked List - 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 

For the no cycle case:

Input

5 -1

Output

No cycle
Linked List - 5 

I understood it! But how much length of cycle need to be created!! There is no information given about it in question!! Please tell me about this!

For example, in the first testcase 50 had next as 48 which made a cycle, so the next of 50 was made as NULL.

Accha! It means we have given ascending order of inputs ??

No, not always. The order basically tells the next value for the node (starting from the head).

But then , don’t you think there can be element of 48 after 50 without a cycle!! I means I can have a duplicate elements!( Without consecutive)!

For example, 1 2 3 4 5 3 8 7 it is also possible without cycle?? What say?

No, there are no duplicate elements.
If an element has already occurred then any further occurrence will mean the same previous node.

1 Like

Ok! Got it sir! Thank you!! And please mention about this in question!

This is not a possible case, please see that then we will have two different ‘next’ for 3.

Is it valid input then?? You just said duplicates are not allowed and if it is in end, then it will form cycle?

Yes it is not a valid input, as I said above.

Okay! Got it then! Thank you

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.