Run time error Last executed input: {1,2}, 1


  • 2
    U

    Hi,

    For unknown reason, I got run time error with input {1,2}, 1. However, I tested in my computer and it works ok...

    class Solution {
    public:
    ListNode *rotateRight(ListNode *head, int k) {
        
        ListNode *cur, *tail, *new_head;
        int n = k, len = 0;
        
        if (!head)
            return NULL;
        if (!head->next)
            return head;
        if (k == 0)
            return head;
        
        // {1,2}, 2, {1,2}, 1
        while (cur) {
            cur = cur->next;
            len++;
        }
        if (len == k)
            return head;
        if (len < k)
            n = k % len;
            
        // keep 2 pointers
        cur = head;
        tail = head;
        
        while (n--) {
            tail = tail->next;
           
            if (!tail->next)
                break;
        }
        while (tail->next) {
            tail = tail->next;
            cur = cur->next;
        }
        new_head = cur->next;
        cur->next = NULL;
        tail->next = head;
        return new_head;
    }
    
    };

  • 1
    F

    I think the case is wrong!


  • 0
    S
    This post is deleted!

Log in to reply
 

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