I don't know what's wrong, but it can't submit with Runtime Error. Can someone help me? Thanks a lot


  • 0
    C
    struct ListNode* oddEvenList(struct ListNode* head) {
        struct ListNode *p; //odd
        struct ListNode *q; //even
        struct ListNode *h1;
        struct ListNode *hp;
        struct ListNode *hq;
        int num=1;
        p=head;
        q=head->next;
       h1=q->next;
       hq=q;
      while(h1!=NULL){
        if(num%2!=0){
            p->next=h1;
            p=h1;
            }
        else{
             q->next=h1;
             q=h1;
        }
        h1=h1->next;
        num++;
    }
    q->next=NULL;
    p->next=hq;
    return hp;
    

    }


  • 0
    Z

    If the input is an empty list, then head == NULL, so you program "q = head->next" while be error.

    Before you use a pointer, make sure it is not NULL.


Log in to reply
 

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