Time out when input: {1, 2, 3} 1??


  • 0
    C
    public class Solution {
    public ListNode rotateRight(ListNode head, int n) {
        if(head == null || head.next == null || n == 0)
            return head;
        int counter = 1; 
        ListNode cur =head;
        for(; cur.next != null; counter++, cur = cur.next);
        cur.next = head;
        cur = cur.next;
        for(int i = 0; i < counter - n - 1; cur = cur.next);
        ListNode ret = cur.next;
        ret.next = head;
        cur.next = null;
        return ret;
    }
    

    }


Log in to reply
 

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