public ListNode rotateRight(ListNode head, int n) {
        if (head == null || head.next == null || n == 0) {
            return head;
        }

        ListNode tail = head;
        int len = 1;

        while (tail.next != null) {
            len++;
            tail = tail.next;
        }
        int shift = n % len;

        if (shift == 0) {
            return head;
        }

        ListNode p = head;
        int i = 1;
        while (i < len - shift) {
            p = p.next;
            i++;
        }
        tail.next = head;
        head = p.next;
        p.next = null;

        return head;
    }