Java clear solution


  • 0
    V
    public class Solution {
    public static int getSize(ListNode head){
        int size=0;
        while(head!=null)
        {
                head=head.next;
                size++;
        }
        return size;
    }
    public ListNode rotateRight(ListNode head, int k) {
        if(head==null)
           return null;
        k%=getSize(head);
        ListNode node=head;
        ListNode newHead=head;
        while(k-->0)
            node=node.next;
        if(node==null)
            return head;
        while(node.next!=null)
        {
            head=head.next;
            node=node.next;
        }
        node.next=newHead;
        node=head.next;
        head.next=null;
        return node;
    }
    

    }


Log in to reply
 

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