# Reverse node of loop of link list ??

142 views
``````List => 1 --->  2 --->  3 --->  4 ---> 5 ---> 6
/        \
10          7
\          /
9 <---  8

OURTPUT => 1 --->  2 --->  3 --->  4 ---> 10 ---> 9
/        \
5           8
\          /
6 <---  7
``````
posted Nov 11, 2013

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* 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;
{
break;
}
}
}
``````