Created
June 27, 2014 14:19
-
-
Save walkingtospace/33f186c67c3af205a12a to your computer and use it in GitHub Desktop.
rotate Linkedlist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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