why is this solution wrong? please help me with this.


  • 0
    D

    class Solution {
    public:
    ListNode* partition(ListNode* head, int x) {
    struct ListNode* lh=NULL,*lt=NULL,*gh=NULL,*gt=NULL,*eh=NULL,*et=NULL;
    while(head!=NULL)
    {
    if(head->val<x)
    {
    if(lh==NULL)
    {
    lh=head;
    lt=head;
    }
    else
    {
    lt->next=head;
    lt=lt->next;
    }
    }
    if(head->val==x)
    {
    if(eh==NULL)
    {
    eh=head;
    et=head;
    }
    else
    {
    et->next=head;
    et=et->next;
    }
    }
    if(head->val>x)
    {
    if(gh==NULL)
    {
    gh=head;
    gt=head;
    }
    else
    {
    gt->next=head;
    gt=gt->next;
    }

          }
          head=head->next; 
          
        }
        
        if(gt!=NULL)
        {
            gt->next=NULL;
        }
        if(lh==NULL)
        {
            if(eh==NULL)
            {
                return gh;
            }
            else
            {
            et->next=gh;
            return eh;
        }
        }
        if(eh==NULL)
        {
            lt->next=gh;
            return lh;
        }
        lt->next=eh;
        et->next=gh;
        return lh;
    }
    

    };```


Log in to reply
 

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