My AC Code ,is it right?


  • 0
    L
    public int getLength(ListNode head){
        int len=0;
        while(head!=null){
            len++;
            head=head.next;
        }
        return len;
    }
    public ListNode rotateRight(ListNode head, int n) {
        int lens=getLength(head);
        if(head==null||head.next==null||n==0||n%lens==0) return head;
        lens=lens-n%lens;
        ListNode p=head;
        int count=0;
        while(p!=null&&count<lens-1){
            p=p.next;
            count++;
        }
        ListNode t=p.next,q;
        p.next=null;
        q=t;
        while(t.next!=null) t=t.next;
        t.next=head;
        return q;
    }

Log in to reply
 

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