Skip to content

Instantly share code, notes, and snippets.

@walkingtospace
Created June 27, 2014 14:19
Show Gist options
  • Save walkingtospace/33f186c67c3af205a12a to your computer and use it in GitHub Desktop.
Save walkingtospace/33f186c67c3af205a12a to your computer and use it in GitHub Desktop.
rotate Linkedlist
//Given a list, rotate the list to the right by k places, where k is non-negative.
Node* rotateNode(Node* head, int k) {
if( k<= 0) return head;
Node* first = kth = head;
Node* end = head;
for(int i=0; i<k+1; i++) kth = kth->next;
while(kth != NULL) {
end = first;
first = first->next;
kth = kth->next;
}
end->next = NULL;
kth->next = head;
return first;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment