Solution To Rotate List ~


  • 0
    Y
    public class Solution {
    public ListNode rotateRight(ListNode head, int n) {
        if(head==null)
        	return head;
        ListNode fast = head;
        ListNode slow = head;
        int len = 1;
        for(int i=0;i<n;i++){
        	fast = fast.next;
        	if(fast==null)
        		return len==n?head:rotateRight(head,(n%len));
        	len++;
        }
        while(fast.next!=null){
        	fast = fast.next;
        	slow = slow.next;
        }
        fast.next = head;
        head = slow.next;
        slow.next = null;
        return head;
    }
    

    }


Log in to reply
 

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