For some reason C# solution is too slow for the judge.

public ListNode RotateRight(ListNode head, int k)
        {
            if (head == null) return head;

            var tail = head;
            int count = 1;

            while (tail.next != null)
            {
                tail = tail.next;
                count++;
            }

            var last = tail;
            k %= count;
            tail = head;
            count -= k;

            if (count == 0) return head;

            while(count> 0)
            {
                tail = tail.next;
                count--;
            }

            ListNode kth = tail;
            last.next = head;

            return kth;
        }
Comments (0)