Simple C++ solution using 2 vectors

interview-questions
Tags: #<Tag:0x00007f242701ba60>

#1

/**

  • Definition for singly-linked list.

  • struct ListNode {

  • int val;
    
  • ListNode *next;
    
  • ListNode(int x) : val(x), next(NULL) {}
    
  • };
    /
    ListNode
    Solution::partition(ListNode* a, int x) {

    vector first;
    vector third;
    ListNode *head=a;
    ListNode *b=a;
    ListNode *c=a;
    while(b!=NULL)
    {
    if(b->val<x)
    first.push_back(b->val);
    else if(b->val>=x)
    third.push_back(b->val);
    b=b->next;
    }
    for(int i=0;i<first.size();i++)
    {
    c->val=first[i];
    c=c->next;
    }

    for(int i=0;i<third.size();i++)
    {
    c->val=third[i];
    c=c->next;
    }
    return head;
    }