easy java solution--only 1ms


  • 0
    K

    public ListNode rotateRight(ListNode head, int k) {
    if (head == null || head.next == null)
    return head;
    ListNode helper = new ListNode(0);
    helper.next = head;
    ListNode cur = head;
    int len = 1;
    while (cur.next != null) {
    cur = cur.next;
    len++;
    }
    ListNode tail = cur;
    k %= len;
    int position = len - k;
    cur = head;
    while (position-- > 1)
    cur = cur.next;
    if (cur.next == null)
    return head;
    helper.next = cur.next;
    cur.next = null;
    tail.next = head;
    return helper.next;
    }

    schwein~~


Log in to reply
 

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