Rotate ListMar 28 '12
Given a list, rotate the list to the right by k places, where k is non-negative.
For example:
Given
return
Given
1->2->3->4->5->NULL
and k = 2
,return
4->5->1->2->3->NULL
.
Analysis
An alternative method to handle the case where k>n is to not wrap around the faster ptr but to traverse the list once to get number of elements in the list and then adjust k=k%n or n
No comments:
Post a Comment