[C++] Test cases are correct but I get "Runtime Error, Last Expected Input: []" upon submission

  • 0

    Here's my code. Does anyone see what's wrong? Any test cases I try work fine.

     class Solution {
            ListNode* oddEvenList(ListNode* head) {
                ListNode* tail = head;
                while (tail->next != nullptr){//get ptr to last node
                    tail = tail->next;
                ListNode* origTail = tail;//the "original" tail. this is where we want to stop.
                ListNode* current = head;//our iterator
                bool go = true;
                while ((current != origTail) && go){
                    tail->next = current->next;//put next node on end of list
                    tail = current->next;//set tail to that node
                    if (current->next == origTail)//if the next node is our original tail, we're done
                        go = false;
                    current->next = current->next->next;//current should point to the node after the next one, i.e., the next odd node
                    current = current->next;//that node is now our current
                    tail->next = nullptr;
                return head;

  • 1

    Your code will break if head is NULL (i.e. empty list)
    Add to the start of your method:

    if (!head) return head;

Log in to reply

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