c# solution


  • 0
    R
    public static ListNode RotateRight(ListNode head, int k)
    	{
    		
    		if(head==null || head.next == null) return head;
    		ListNode tail = head, current = head;
    		int length = 1;
    		while (tail.next != null)
    		{
    			length++;
    			tail = tail.next;
    		}
    		k = k % length;
    		if(k==0) return head;
    		for (int i = 0; i < length-k-1; i++)
    		{
    			current = current.next;
    		}
    		
    		var newHead = current.next;
    		tail.next = head;
    		current.next = null;
    		return newHead;
    	}
    

Log in to reply
 

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