Accepted as best submission in C, very intuitive - comments ignored - needless


  • 0
    struct ListNode* partition(struct ListNode* head, int x)
    {
        if(!head) return NULL;
        struct ListNode *less = (struct ListNode*)malloc(sizeof(struct ListNode)); //used as head;
        struct ListNode *greater = (struct ListNode*)malloc(sizeof(struct ListNode));
        struct ListNode *l=less, *g=greater; //used to track the chain for connecting;
        while(head)
        {
            if(head->val < x)
            {
                l->next = head;
                l = l->next;
            }
            else
            {
                g->next = head;
                g = g->next;
            }
            head = head->next;
        }
        g->next = NULL;
        l->next = greater->next;
        return less->next;
    }

Log in to reply
 

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