# the java solution

• I guess I still have a long way to go...Σ( ￣□￣||)
/**

• Definition for singly-linked list.

• public class ListNode {

• ``````int val;
``````
• ``````ListNode next;
``````
• ``````ListNode(int x) { val = x; }
``````
• }
*/
public class Solution {
public ListNode rotateRight(ListNode head, int k) {
if(k == 0)
{
return head;
}
if(head == null||head.next == null)
{
//in case the head is null or there are only one Node in the List.
return head;
}
int length = 0;
ListNode number = head;
while(number != null)
{
//get the length of the list
number = number.next;
length++;
}
while(length<=k)
{
//in case the k is larger than length
k = k - length;
}
// k = k%length;
// k = length - k;
for(int i = 0;i<k;i++)
{
//rotate k times
head = rotate(head);
}
return head;

}
public ListNode rotate(ListNode head)
{
if(head == null||head.next == null)
{
//in case the head is null or there are only one Node in the List.
return head;
}
ListNode end =head;//the end of the List
ListNode previous = head;// the node previous to the end
while(end.next !=null)
{
//get previous and end
previous = end;
end = end.next;
}

`````` end.next = head;//set end to be the head
previous.next = null;//and the node previous to the end becomes end now
return end;
``````

}
}

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.