@@ -0,0 +1,1664 @@
+/**%0A *%09Given a list, rotate the list to the right by k places, where k is non-negative.%0A *%0A *%09For example:%0A *%0A *%09Given 1-%3E2-%3E3-%3E4-%3E5-%3ENULL and k = 2,%0A *%0A *%09return 4-%3E5-%3E1-%3E2-%3E3-%3ENULL.%0A *%0A *%09Author: matao%0A *%0A *%09%E6%B3%A8%E6%84%8Fk %E5%A4%A7%E4%BA%8E %E9%93%BE%E8%A1%A8%E9%95%BF%E5%BA%A6%E6%97%B6%E7%9A%84%E5%A4%84%E7%90%86%0A */%0A%0Aclass ListNode %7B%0A%09int val;%0A%09ListNode next;%0A%09ListNode(int x) %7B val = x; %7D%0A%7D%0A%0Apublic class RotateList %7B%0A%09public static ListNode rotateRight(ListNode head, int k) %7B%0A%09%09if (head == null) %7B%0A%09%09%09return null;%0A%09%09%7D%0A%09%09if(k == 0) %7B%0A%09%09%09return head;%0A%09%09%7D%0A%09%09k = k %25 getLength(head);%0A%09%09ListNode dummy = new ListNode(0);%0A%09%09dummy.next = head;%0A%09%09head = dummy;%0A%09%09ListNode tail = dummy;%0A%09%09while(k-- %3E 0) %7B%0A%09%09%09tail = tail.next;%0A%09%09%7D%0A%09%09while(tail.next != null) %7B%0A%09%09%09head = head.next;%0A%09%09%09tail = tail.next;%0A%09%09%7D%0A%09%09tail.next = dummy.next;%0A%09%09dummy.next = head.next;%0A%09%09head.next = null;%0A%09%09return dummy.next;%0A    %7D%0A%0A    public static int getLength(ListNode head) %7B%0A        int length = 0;%0A        while (head != null) %7B%0A            length ++;%0A            head = head.next;%0A        %7D%0A        return length;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A    %09ListNode head = new ListNode(0);%0A%09%09ListNode tail = head;%0A%09%09for (int i = 1; i %3C= 5; i++) %7B%0A%09%09%09ListNode newNode = new ListNode(i);%0A%09%09%09tail.next = newNode;%0A%09%09%09tail = tail.next;%0A%09%09%7D%0A%09%09// ListNode node2 = new ListNode(2);%0A%09%09// ListNode node1 = new ListNode(1);%0A%09%09// tail.next = node2;%0A%09%09// node2.next = node1;%0A%0A%09%09ListNode p = head.next;%0A%09%09while(p != null) %7B%0A%09%09%09System.out.print(p.val + %22 %22);%0A%09%09%09p = p.next;%0A%09%09%7D%0A%09%09System.out.println();%0A%0A%09%09ListNode result = rotateRight(head.next, 2);%0A%09%09while(result != null) %7B%0A%09%09%09System.out.print(result.val + %22 %22);%0A%09%09%09result = result.next;%0A%09%09%7D%0A%09%09System.out.println();%0A    %7D%0A%7D

add Rotate List solution