Very easy to understand O(n) JAVA solution


  • 0
    J
    class Solution 
    {
        public ListNode rotateRight(ListNode head, int k) 
        {
            if (head == null) return null;
            ListNode tail = head;
            ListNode newTail = head;
            int len = 1;
            while (tail.next != null) 
            {
                tail = tail.next;
                len++;
            }
            for (int i = 0; i < len - k%len - 1; i++) newTail = newTail.next;
            tail.next = head;
            head = newTail.next;
            newTail.next = null;
            return head;
        }     
    }
    

Log in to reply
 

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