my-java-solution


  • 0

    class Solution {
    public ListNode rotateRight(ListNode head, int k) {
    ListNode p=head;
    ListNode q=p;
    int index=0;
    if(head==null||k==0)
    return head;
    while(index<k&&q.next!=null){
    q=q.next;
    index++;
    }
    if(index<k){
    k=k%(index+1);
    if(k==0)
    return head;
    index=0;
    q=p;
    while(index<k){
    q=q.next;
    index++;
    }
    }
    while(q.next!=null){
    q=q.next;
    p=p.next;
    }
    ListNode h=p.next;
    q.next=head;
    p.next=null;
    return h;
    }
    }


Log in to reply
 

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