Time limit exceeded on input [2,1,4,2,2], 4?


  • 0
    W
    struct ListNode* partition(struct ListNode* head, int x) {
       if(head==NULL)
       return head;
       struct ListNode* start=(struct ListNode*)malloc(sizeof(struct ListNode));
       struct ListNode* temp1=head;
       struct ListNode* t=start;
        struct ListNode* t1=start;
       struct ListNode* temp=start;
        struct ListNode* temp2=head;
        while(temp1!=NULL)
        {
            if(temp1->val<x)
            {
                start->val=temp1->val;
                start->next=(struct ListNode*)malloc(sizeof(struct ListNode));
                start=start->next;
            }
            temp1=temp1->next;
        }
         while(temp2!=NULL)
        {
            if(temp2->val>=x)
            {
                start->val=temp2->val;
                start->next=(struct ListNode*)malloc(sizeof(struct ListNode));
                start=start->next;
            }
            temp2=temp2->next;
        }
        while(t->next->next!=NULL)
        t=t->next;
        t->next=NULL;
      
        return t1;
    }

Log in to reply
 

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