public static ListNode rotateRight(ListNode head, int n) {//exceeds time limit
        if (head == null || n == 0) {
            return head;
        }
        //first, count the length
        int len = 1;
        ListNode temp = head;
        while (temp.next != null) {
            ++len;
            temp = temp.next;
        }
        //n should be the mod of len;
        if (len <= n) {
            n = n % len;
        }
        //then, rotate
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode p = head;
        ListNode newhead = head;
        ListNode newhead_prev = dummy;
        while (n > 0) {
            newhead = newhead.next;
            newhead_prev = newhead_prev.next;
            --n;
        }
        while (newhead.next != null) {
            p = p.next;
            newhead = newhead.next;
        }
        temp.next = head;
        newhead_prev.next = null;
        return newhead;
    }