void rotate struct node head_ref int int struct node currNode head_ref

 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
void rotate(struct node **head_ref, int k)
{
int n = 1;
struct node *currNode = (*head_ref);
while(currNode->next)
currNode = currNode->next, n++;
k %= n;
if(k==0)
return;
currNode = (*head_ref);
int i = 1;
while(i<k)
{
currNode = currNode->next;
i++;
}
struct node* oldHead = (*head_ref);
(*head_ref) = currNode->next;
currNode->next = NULL;
currNode = *head_ref;
while(currNode->next)
currNode = currNode->next;
currNode->next = oldHead;
}