Partitioning a linked list around a given value and keeping the original order
Last Updated : 27 Feb, 2026
Given a linked list and a value x, partition it such that all nodes less than x come first, then all nodes with a value equal to x, and finally nodes with a vaxlue greater than x. The original relative order of the nodes in each of the three partitions should be preserved.
Examples:
Input : 1 -> 4 -> 3 -> 2 -> 5 -> 2 -> 3, x = 3 Output: 1 -> 2 -> 2 -> 3 -> 3 -> 4 -> 5 Explanation: In the below linked list, all nodes with value less than 3 are on the left and rest of the nodes on the right by maintaining the relative order.
[Approach] Three Partition Method – O(n) Time and O(1) Space
The idea is to use three dummy nodes to create three separate partitions: less, equal, and greater. As the list is traversed, each node is added to its corresponding partition. Once all nodes are processed, the partitions are connected to form the final list.
C++
#include<iostream>usingnamespacestd;classNode{public:intdata;Node*next;Node(intval){data=val;next=NULL;}};Node*partition(Node*head,intx){// Dummy head for nodes less than xNode*lessHead=newNode(0);// Dummy head for nodes equal to xNode*equalHead=newNode(0);// Dummy head for nodes greater than or equal to xNode*greaterHead=newNode(0);Node*less=lessHead;Node*equal=equalHead;Node*greater=greaterHead;Node*curr=head;while(curr!=NULL){if(curr->data<x){less->next=curr;less=less->next;}elseif(curr->data==x){equal->next=curr;equal=equal->next;}else{greater->next=curr;greater=greater->next;}curr=curr->next;}// Connect the partitions togethergreater->next=NULL;// Connect equal to greaterequal->next=greaterHead->next;// Connect less to equalless->next=equalHead->next;// New head of the rearranged listNode*newHead=lessHead->next;// Clean up dummy nodesdeletelessHead;deleteequalHead;deletegreaterHead;returnnewHead;}voidprintList(Node*head){Node*curr=head;while(curr!=NULL){cout<<curr->data<<" ";curr=curr->next;}cout<<endl;}intmain(){// Creating the linked list: 1 -> 4 -> 3 -> 2 -> 5 -> 2Node*head=newNode(1);head->next=newNode(4);head->next->next=newNode(3);head->next->next->next=newNode(2);head->next->next->next->next=newNode(5);head->next->next->next->next->next=newNode(2);intx=3;head=partition(head,x);printList(head);return0;}
C
#include<stdio.h>#include<stdlib.h>structNode{intdata;structNode*next;};structNode*createNode(intval);structNode*partition(structNode*head,intx){// Dummy head for nodes less than xstructNode*lessHead=createNode(0);// Dummy head for nodes equal to xstructNode*equalHead=createNode(0);// Dummy head for nodes greater than or equal to xstructNode*greaterHead=createNode(0);structNode*less=lessHead;structNode*equal=equalHead;structNode*greater=greaterHead;structNode*curr=head;while(curr!=NULL){if(curr->data<x){less->next=curr;less=less->next;}elseif(curr->data==x){equal->next=curr;equal=equal->next;}else{greater->next=curr;greater=greater->next;}curr=curr->next;}// Connect the partitions togethergreater->next=NULL;// Connect equal to greaterequal->next=greaterHead->next;// Connect less to equalless->next=equalHead->next;// New head of the rearranged liststructNode*newHead=lessHead->next;// Clean up dummy nodesfree(lessHead);free(equalHead);free(greaterHead);returnnewHead;}voidprintList(structNode*head){structNode*curr=head;while(curr!=NULL){printf("%d ",curr->data);curr=curr->next;}printf("\n");}structNode*createNode(intval){structNode*newNode=(structNode*)malloc(sizeof(structNode));newNode->data=val;newNode->next=NULL;returnnewNode;}intmain(){// Creating the linked list: 1 -> 4 -> 3 -> 2 -> 5 -> 2structNode*head=createNode(1);head->next=createNode(4);head->next->next=createNode(3);head->next->next->next=createNode(2);head->next->next->next->next=createNode(5);head->next->next->next->next->next=createNode(2);intx=3;head=partition(head,x);printList(head);return0;}
Java
classNode{intdata;Nodenext;Node(intval){data=val;next=null;}}classGfG{staticNodepartition(Nodehead,intx){// Dummy head for nodes less than xNodelessHead=newNode(0);// Dummy head for nodes equal to xNodeequalHead=newNode(0);// Dummy head for nodes greater than or equal to xNodegreaterHead=newNode(0);Nodeless=lessHead;Nodeequal=equalHead;Nodegreater=greaterHead;Nodecurr=head;while(curr!=null){if(curr.data<x){less.next=curr;less=less.next;}elseif(curr.data==x){equal.next=curr;equal=equal.next;}else{greater.next=curr;greater=greater.next;}curr=curr.next;}// Connect the partitions togethergreater.next=null;// Connect equal to greaterequal.next=greaterHead.next;// Connect less to equalless.next=equalHead.next;// New head of the rearranged listNodenewHead=lessHead.next;returnnewHead;}staticvoidprintList(Nodehead){Nodecurr=head;while(curr!=null){System.out.print(curr.data+" ");curr=curr.next;}System.out.println();}publicstaticvoidmain(String[]args){// Creating the linked list: 1 -> 4 -> 3 -> 2 -> 5 -> 2Nodehead=newNode(1);head.next=newNode(4);head.next.next=newNode(3);head.next.next.next=newNode(2);head.next.next.next.next=newNode(5);head.next.next.next.next.next=newNode(2);intx=3;head=partition(head,x);printList(head);}}
Python
classNode:def__init__(self,val):self.data=valself.next=Nonedefpartition(head,x):# Dummy head for nodes less than xlessHead=Node(0)# Dummy head for nodes equal to xequalHead=Node(0)# Dummy head for nodes greater than or equal to xgreaterHead=Node(0)less=lessHeadequal=equalHeadgreater=greaterHeadcurr=headwhilecurrisnotNone:ifcurr.data<x:less.next=currless=less.nextelifcurr.data==x:equal.next=currequal=equal.nextelse:greater.next=currgreater=greater.nextcurr=curr.next# Connect the partitions togethergreater.next=None# Connect equal to greaterequal.next=greaterHead.next# Connect less to equalless.next=equalHead.next# New head of the rearranged listnewHead=lessHead.nextreturnnewHeaddefprintList(head):curr=headwhilecurrisnotNone:print(curr.data,end=" ")curr=curr.nextprint()if__name__=="__main__":# Creating the linked list: 1 -> 4 -> 3 -> 2 -> 5 -> 2head=Node(1)head.next=Node(4)head.next.next=Node(3)head.next.next.next=Node(2)head.next.next.next.next=Node(5)head.next.next.next.next.next=Node(2)x=3head=partition(head,x)printList(head)
C#
usingSystem;classNode{publicintdata;publicNodenext;publicNode(intval){data=val;next=null;}}classGfG{staticNodePartition(Nodehead,intx){// Dummy head for nodes less than xNodelessHead=newNode(0);// Dummy head for nodes equal to xNodeequalHead=newNode(0);// Dummy head for nodes greater than or equal to xNodegreaterHead=newNode(0);Nodeless=lessHead;Nodeequal=equalHead;Nodegreater=greaterHead;Nodecurr=head;while(curr!=null){if(curr.data<x){less.next=curr;less=less.next;}elseif(curr.data==x){equal.next=curr;equal=equal.next;}else{greater.next=curr;greater=greater.next;}curr=curr.next;}// Connect the partitions togethergreater.next=null;// Connect equal to greaterequal.next=greaterHead.next;// Connect less to equalless.next=equalHead.next;// New head of the rearranged listNodenewHead=lessHead.next;returnnewHead;}staticvoidPrintList(Nodehead){Nodecurr=head;while(curr!=null){Console.Write(curr.data+" ");curr=curr.next;}Console.WriteLine();}staticvoidMain(string[]args){// Creating the linked list: 1 -> 4 -> 3 -> 2 -> 5 -> 2Nodehead=newNode(1);head.next=newNode(4);head.next.next=newNode(3);head.next.next.next=newNode(2);head.next.next.next.next=newNode(5);head.next.next.next.next.next=newNode(2);intx=3;head=Partition(head,x);PrintList(head);}}
JavaScript
classNode{constructor(val){this.data=val;this.next=null;}}functionpartition(head,x){// Dummy head for nodes less than xletlessHead=newNode(0);// Dummy head for nodes equal to xletequalHead=newNode(0);// Dummy head for nodes greater than or equal to xletgreaterHead=newNode(0);letless=lessHead;letequal=equalHead;letgreater=greaterHead;letcurr=head;while(curr!==null){if(curr.data<x){less.next=curr;less=less.next;}elseif(curr.data===x){equal.next=curr;equal=equal.next;}else{greater.next=curr;greater=greater.next;}curr=curr.next;}// Connect the partitions togethergreater.next=null;// Connect equal to greaterequal.next=greaterHead.next;// Connect less to equalless.next=equalHead.next;// New head of the rearranged listletnewHead=lessHead.next;returnnewHead;}functionprintList(head){letcurr=head;while(curr!==null){console.log(curr.data);curr=curr.next;}console.log();}// Creating the linked list: 1 -> 4 -> 3 -> 2 -> 5 -> 2lethead=newNode(1);head.next=newNode(4);head.next.next=newNode(3);head.next.next.next=newNode(2);head.next.next.next.next=newNode(5);head.next.next.next.next.next=newNode(2);letx=3;head=partition(head,x);printList(head);