Leetcode-Rotate List(Java)

Question:

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.

Thinking:

Firstly, count the length of the linked list. Then get the n-k th node. Finnaly, change the pointer of it and the tail.

Solution:

public ListNode rotateRight(ListNode head, int k) {
    if (head == null || head.next == null)    return head;
    ListNode p = head;
    int count = 1;
    while (p.next != null) {
        p = p.next;
        count++;
    }
    if (k == count)    return head;
    p.next = head;
    p = head;
    for (int i = 1; i < count - k%count; i++) {
        p = p.next;
    }
    ListNode nhead = p.next;
    p.next = null;

    return nhead;
}