4ms C code using pointer to pointer


  • 1
    B

    Extra cost is imposed by moving even back and forth

    void b_b4_a(struct ListNode **a, struct ListNode **b){
        struct ListNode *t=*b;
        *b = t->next;
        t->next = *a;
        *a = t;
    }
    
    struct ListNode* oddEvenList(struct ListNode* head) {
        struct ListNode *t, **even, **odd;
        
        if(t==NULL || t->next==NULL || t->next->next==NULL) return head;
        even=&(t->next);
        odd=&(head->next->next);
        while(1){
            b_b4_a(even, odd);
            t=*odd;
            if(t==NULL || t->next==NULL) break;
            even=&((*even)->next);
            odd=&(t->next);
        }
        return head;
    }

Log in to reply
 

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