Program C Time Limit Exceeded


  • 0
    J

    /**

    • Definition for singly-linked list.

    • struct ListNode {

    • int val;
      
    • struct ListNode *next;
      
    • };

    */

    struct ListNode* oddEvenList(struct ListNode* head) {

    if(head == NULL){
        return NULL;
    }
    
    if(head->next == NULL){
        return head;
    }
    
    struct ListNode* odd = head;
    struct ListNode* even = head->next;
    struct ListNode* current = even->next;
    struct ListNode* evenhead = head->next;
    struct ListNode* oddhead = odd;
    int flag = 1;
    
    while(current!=NULL){
        struct ListNode* newone = (struct ListNode*) malloc(sizeof(struct ListNode));
        newone->val = current->val;
        //printf("test: %d\n", newone->data);
        if(flag==1){
            odd->next = newone;
            odd = newone;
            //printf("test: %d\n", odd->data);
            current = current->next;
            flag = 0;
        }else{
            //printf("test: %d\n", even->data);
            even->next = newone;
            even = newone;
            //printf("test: %d\n", even->data);
            current = current->next;
            flag = 1;
        }
    }
    
    odd->next = evenhead;
    //printf("test evenhead: %d\n", evenhead->data);
    return oddhead;
    

    }


Log in to reply
 

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