My AC Solution


  • 0
    D
        public ListNode rotateRight(ListNode head, int k) {
            if (head == null || head.next == null || k ==0) {return head;}
            int len = 0;
            ListNode temp = head, slow= head, fast = head;
            ListNode newHead = null;
            while (temp != null) {
                temp = temp.next;
                len++;
            }
            k = k % len;
            if (k==0) {return head;}
            for (int i = 0; i < k; i++) {
                fast = fast.next;
            }
            while (fast.next != null) {
                fast = fast.next;
                slow = slow.next;
            }
            newHead = slow.next;
            fast.next = head;
            slow.next = null;
            return newHead;
        }

Log in to reply
 

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