JAVA solution


  • 0
    C
    public ListNode rotateRight(ListNode head, int k) {
        if(head == null || head.next == null) return head;
        ListNode cur = head;
        ListNode seclist = head;
        int size = 1;
        while(cur.next != null){
            cur = cur.next;
            size++;
        }
        k = k % size;
        for(int i = 1; i < size - k ;i++){
            seclist = seclist.next;
        }
        cur.next = head;
        head = seclist.next;
        seclist.next = null;
        return head;
    }

Log in to reply
 

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