Could not solve the question using linked list....even the given solution code doesn't seem to be working

Can you please review the code that i have typed and explain to me what was wrong…or correct the code

@debprotim,

Say If INPUT IS 1 2 3 4 5 2 3 -1

Form a linked list 1->2->3->4>5 then you see that 2 has already been visited , so connect the next of 5 to 2 (which has already been created). This way you have created the circular linked list . After that just detect the cycle, remove the cycle, print the new linked list.

Maintain a map or a visited array…whenever you see that a node is already visited, connect next of previous node to that node

What you are doing is, you are creating a linear linked list of 7 elements ( for the sample input).