Rotate the linked list

class Solution {

      public ListNode rotateRight(ListNode head, int k) {
      if(head==null)return head;
    
         ListNode temp=head, prev=null;
         int len=1;
        while(temp.next!=null){
             len++;
             temp=temp.next;
       }
       k=k%len;
       int pos=len-k; 
       temp.next=head;
    
       for(int i=1;i<pos;i++){
       head=head.next;
    }
    temp=head.next;
    head.next=null;
    
    return temp;
        }
		}
Comments (2)