I guess this is the correct output for that is
OUTPUT => 1 ---> 2 ---> 3 ---> 4 ---> 5 ---> 10 ---> 9 ---> 8 ---> 7 ---> 6 ---> [[Back to 4]]
If that is the case then first
Detect loop size in linked list by using Floyd’s Cycle detection algorithm i.e, create 2 pointers one fast and other slow pointing to start of node every time you run one iteration fast will move 2 points ahead and slow will move 1 when two pointers point to same node break.
Now fix one pointer and move other to detect the length of cycle, let it be n
Now we need to find starting point of cycle this can be done by pointing one at start and other at n nodes ahead then when two points point to same node it will be start of cycle..
Now if we reverse this circular linked list i.e, cycle we are done..
Code to reverse circular linked list
void reverse(Node** headRef)
{
Node* result = NULL;
Node* current = *headRef;
Node* next;
while (current != NULL)
{
next = current->next; // tricky: note the next node
current->next = result; // move the node onto the result
result = current;
current = next;
if (current == *headRef)
{
break;
}
}
(*headRef)->next = result;
*headRef = result;
}
Credits to reverse circular linked list : Circular Link List