Partition a Linked List around a given value | Practice | GeeksforGeeks

x = 3
Output:
1->2->2->3->3->4->5
Explanation: 
Nodes with value less than 3 come first, 
then equal to 3 and then greater than 3.

Input:
1->4->2->10 
x = 3
Output: 
1->2->4->10
Explanation:
Nodes with value less than 3 come first,
then equal to 3 and then greater than 3.

+1
madduke
This comment was deleted.
0
rainx3 days ago
EASIEST SOLUTION
 
 
void PRINT(Node* head){
    Node* temp=head;
    temp=temp->next;
    while(temp!=NULL){
        cout<<temp->data<<" ";
        temp=temp->next;
    }
    cout<<endl;
    return;
}
struct Node* partition(struct Node* head, int x) {
    Node* lessX=new Node(-1);   Node* PlessX=lessX;
    Node* equalX=new Node(-1);  Node* PequalX=equalX;
    Node* greatX=new Node(-1);  Node* PgreatX=greatX;
    
    Node* temp=head;
    
    while(temp!=NULL){
        if(temp->data==x){
            PequalX->next=temp;
            PequalX=PequalX->next;
        }
        else if(temp->data>x){
            PgreatX->next=temp;
            PgreatX=PgreatX->next;
        }
        else{
            PlessX->next=temp;
            PlessX=PlessX->next;
        }
        temp=temp->next;
    }
    PlessX->next=NULL;
    PequalX->next=NULL;
    PgreatX->next=NULL;
    
    // PRINT(lessX);
    // PRINT(equalX);
    // PRINT(greatX);
    
    if(lessX->next==NULL){
        if(equalX->next==NULL){
            return greatX->next;
        }
        else{
            PequalX->next=greatX->next;
            return equalX->next;
        }
    }
    if(equalX->next==NULL){
        PlessX->next=greatX->next;
        return lessX->next;
    }
    PlessX->next=equalX->next;
    PequalX->next=greatX->next;
    return lessX->next;
}
0
ashishpatel39461 week ago
Simple java solution
 
class Solution {   public static Node partition(Node node, int x) {       Node a = new Node(0); Node h1 = a;       Node b = new Node(0); Node h2 = b;       Node c = new Node(0); Node h3 = c;       Node curr = node;       while(curr!=null){           if(curr.data<x){               Node temp = new Node (curr.data);               a.next = temp; a=temp;           }            if(curr.data==x){               Node temp = new Node (curr.data);               b.next = temp; b=temp;           }            if(curr.data>x){               Node temp = new Node (curr.data);               c.next = temp; c=temp;           }           curr = curr.next;       } h1=h1.next; h2=h2.next; h3 = h3.next;             if(h1!=null&&h2==null&&h3==null) return h1;      else if(h1==null&&h2!=null&&h3==null) return h2;      else if(h1==null&&h2==null&&h3!=null) return h3;      else if(h1!=null&&h2!=null&&h3!=null){           a.next = h2;           b.next=h3;           return h1;       }       else if(h1!=null&&h2!=null){           a.next = h2;           return h1;       }       else if(h1!=null&&h3!=null){           a.next = h3;           return h1;       }       else{       b.next=h3;           return h2;       }   }}
0
subhashishde08
This comment was deleted.
0
roshansinghns1 week ago
struct Node* partition(struct Node* head, int x) {
    // code here
    vector<int> small;
    vector<int> equal;
    vector<int> large;
    Node* tmp=head;
    while(tmp!=nullptr)
    {
        if(tmp->data<x)
            small.push_back(tmp->data);
        else
        if(tmp->data==x)
            equal.push_back(tmp->data);
        else
            large.push_back(tmp->data);
        tmp=tmp->next;
    }
    Node* h=nullptr;
    Node* tail=nullptr;
    for(auto i:small)
    {
        Node* t=new Node(i);
        if(h==nullptr)
        {
            h=t;
            tail=h;
        }
        else
        {
            tail->next=t;
            tail=t;
        }
    }
    for(auto i:equal)
    {
        Node* t=new Node(i);
        if(h==nullptr)
        {
            h=t;
            tail=h;
        }
        else
        {
            tail->next=t;
            tail=t;
        }
    }
    for(auto i:large)
    {
        Node* t=new Node(i);
        if(h==nullptr)
        {
            h=t;
            tail=h;
        }
        else
        {
            tail->next=t;
            tail=t;
        }
    }
    return h;
}
0
ankitpodder1 week ago
struct Node* partition(struct Node* head, int x) {
    Node *temp = head , *ans = NULL , *ansTemp = NULL;
    while(temp){
        if(temp->data < x){
            if(ans == NULL){
                ans = new Node(temp->data);
                ansTemp = ans;
            }
            else{
                ansTemp->next = new Node(temp->data);
                ansTemp = ansTemp->next;
            }
        }
        temp = temp->next;
    }
    temp = head;
    while(temp){
        if(temp->data == x){
            if(ans == NULL){
                ans = new Node(temp->data);
                ansTemp = ans;
            }
            else{
                ansTemp->next = new Node(temp->data);
                ansTemp = ansTemp->next;
            }
        }
        temp = temp->next;
    }
    temp = head;
    while(temp){
        if(temp->data > x){
            if(ans == NULL){
                ans = new Node(temp->data);
                ansTemp = ans;
            }
            else{
                ansTemp->next = new Node(temp->data);
                ansTemp = ansTemp->next;
            }
        }
        temp = temp->next;
    }
    
    ansTemp->next = NULL;
    return ans;
}
+1
rahulmallick11151 week ago
struct Node* partition(struct Node* head, int x) {
    Node * greater = NULL, *greater_head = NULL ; 
    Node * smaller = NULL, *smaller_head = NULL ;
    Node * equal   = NULL, *equal_head   = NULL ; 
    while(head){
        if(head -> data > x)
            (greater) ? greater = greater -> next = head  : greater = greater_head = head ;
        else if(head -> data == x)
            (equal)   ? equal   = equal   -> next = head  : equal   = equal_head   = head ;
        else
            (smaller) ? smaller = smaller -> next = head  : smaller = smaller_head = head ;
        head = head -> next ; 
    }
    if(greater){
        greater -> next = NULL ;
    }
    if(equal){
        equal   -> next = greater_head ;
        greater_head    = equal_head   ;
    }
    if(smaller){
        smaller -> next = greater_head ;
        greater_head    = smaller_head ;
    }
    return greater_head ;
}
0
sourav991661 week ago
struct Node* partition(struct Node* head, int x) {
    // code here
    
    vector<int>small;
    vector<int>eqal;
    vector<int>big;
    
    struct Node*cur=head;
    while(cur!=NULL){
        if(cur->data<x) small.push_back(cur->data);
        else if(cur->data==x) eqal.push_back(cur->data);
        else big.push_back(cur->data);
        
        cur=cur->next;
    }
    
    struct Node* newhead=new Node(0);
    head=newhead;
    for(auto x: small){
        head->next=new Node(x);
        head=head->next;
    }
    
    for(auto x: eqal){
        head->next=new Node(x);
        head=head->next;
    }
    
    
    for(auto x:  big){
        head->next=new Node(x);
        head=head->next;
    }
    
    
    
    head->next=NULL;
    return newhead->next;
    
    
}
+1
akashmidha801 week ago
 
just compare the data and make a new list.
simple approach.
 
 
 
 
struct Node* partition(struct Node* head, int x) {   // code here   struct Node *zero = new Node(0);   struct Node *ans = zero;      struct Node *temp = head;      while(temp){       if(temp->data<x){           struct Node *mynode = new Node(temp->data);           zero->next = mynode;           zero = zero->next;       }       temp = temp->next;   }      temp = head;      while(temp){       if(temp->data==x){           struct Node *mynode = new Node(temp->data);           zero->next = mynode;           zero = zero->next;       }       temp = temp->next;   }      temp = head;      while(temp){       if(temp->data>x){           struct Node *mynode = new Node(temp->data);           zero->next = mynode;           zero = zero->next;       }       temp = temp->next;   }      return ans->next;   }
0
hellopreeti01071 week ago
Using 3 linkedlists in c++:
struct Node* partition(struct Node* head, int x) {   if(head==NULL)   return NULL;     struct Node* h1=NULL;     struct Node* h2=NULL;     struct Node* h3=NULL;     struct Node* hl1=NULL;     struct Node* hl2=NULL;     struct Node* hl3=NULL;          struct Node* curr=head;     while(curr!=NULL)     {         if(curr->data<x)         {            if(h1==NULL)            {                h1=curr;                hl1=curr;                            }            else            {                hl1->next=curr;                hl1=hl1->next;                            }         }         else if(curr->data==x)         {             if(h2==NULL)            {                h2=curr;                hl2=curr;                            }            else            {                hl2->next=curr;                hl2=hl2->next;                            }                      }         else         {                          if(h3==NULL)            {                h3=curr;                hl3=curr;                            }            else            {                hl3->next=curr;                hl3=hl3->next;                            }                      }         curr=curr->next;     }     if(hl1!=NULL)     {         if(h2!=NULL)         {             hl1->next=h2;             if(h3!=NULL)             {                 hl2->next=h3;                 hl3->next=NULL;             }             else             hl2->next=NULL;         }         else         {             if(h3!=NULL)             {                 hl1->next=h3;                 hl3->next=NULL;             }             else             {                 hl1->next=NULL;             }         }         return h1;              }     else if(h2!=NULL)     {                      if(h3!=NULL)             {                 hl2->next=h3;                 hl3->next=NULL;             }             else             hl2->next=NULL;             return h2;              }     else     {         hl3->next=NULL;         return h3;     }    }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.