package Level3;
import Utility.ListNode;
/**
*
* Rotate List
*
* Given a list, rotate the list to the right by k places, where k is
* non-negative.
*
* For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL.
*
*/
public class S61 {
public static void main(String[] args) {
ListNode head = new ListNode(1);
ListNode n2 = new ListNode(2);
ListNode n3 = new ListNode(3);
ListNode n4 = new ListNode(4);
ListNode n5 = new ListNode(5);
head.next = n2;
n2.next = n3;
n3.next = n4;
n4.next = n5;
ListNode rotateHead = rotateRight(head, 2);
rotateHead.print();
}
public static ListNode rotateRight(ListNode head, int n) {
if(head == null){
return null;
}
ListNode back = head;
ListNode front = head;
ListNode end = head;
// 先把链表改为循环链表
while(front.next != null){
front = front.next;
}
end = front; // 记录原尾节点
front.next = head; // 形成环
front = head; // 复原front指针
// 使得front在back前面n个距离
for(int i=0; i