Partitioning a linked list around a given value and If we don’t care about making the elements of the list “stable”

Given a linked list and a value x, partition a linked list around a value x, such that all nodes less than x come before all nodes greater than or equal to x. If x is contained within the list the values of x only need to be after the elements less than x (see below). The partition element x can appear anywhere in the “right partition”; it does not need to appear between the left and right partitions.

Similar problem : Partitioning a linked list around a given value and keeping the original order

Examples:

Input :  3 -> 5 -> 10 -> 2 -> 8 -> 2 -> 1 
         x = 5
Output : 1-> 2-> 2-> 3-> 5-> 10-> 8

If we don’t care about making the elements of the list “stable” then we can instead rearrange the elements by growing the list at the head and tail.

In this approach, we start a “new” list (using the existing nodes). Elements bigger than the pivot element are put at the tail and elements smaller are put at the head. Each time we insert an element, we update either the head or tail.

Below is the implementation of above idea.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to partition a linked list around a
// given value.
#include<bits/stdc++.h>
using namespace std;
  
/* Link list Node */
struct Node
{
    int data;
    struct Node* next;
};
  
// A utility function to create a new node
Node *newNode(int data)
{
    struct Node* new_node = new Node;
    new_node->data  = data;
    new_node->next = NULL;
    return new_node;
}
  
// Function to make a new list(using the existing
// nodes) and return head of new list.
struct Node *partition(struct Node *head, int x)
{
    /* Let us initialize start and tail nodes of
    new list */
    struct Node *tail = head;
  
    // Now iterate original list and connect nodes
    Node *curr = head;
    while (curr != NULL)
    {
        struct Node *next = curr->next;
        if (curr->data < x)
        {
            /* Insert node at head. */
            curr->next = head;
            head = curr;
        }
  
        else // Append to the list of greater values
        {
            /* Insert node at tail. */
            tail->next = curr;
            tail = curr;
        }
        curr = next;
    }
    tail->next = NULL;
  
    // The head has changed, so we need
    // to return it to the user.
    return head;
}
  
/* Function to print linked list */
void printList(struct Node *head)
{
    struct Node *temp = head;
    while (temp != NULL)
    {
        printf("%d  ", temp->data);
        temp = temp->next;
    }
}
  
// Driver program to run the case
int main()
{
    /* Start with the empty list */
    struct Node* head = newNode(3);
    head->next = newNode(5);
    head->next->next = newNode(8);
    head->next->next->next = newNode(2);
    head->next->next->next->next = newNode(10);
    head->next->next->next->next->next = newNode(2);
    head->next->next->next->next->next->next = newNode(1);
  
    int x = 5;
    head = partition(head, x);
    printList(head);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to partition a linked list 
// around a given value. 
class GfG { 
  
/* Link list Node */
static class Node 
    int data; 
    Node next; 
}
  
// A utility function to create a new node 
static Node newNode(int data) 
    Node new_node = new Node(); 
    new_node.data = data; 
    new_node.next = null
    return new_node; 
  
// Function to make a new list 
// (using the existing nodes) and 
// return head of new list. 
static Node partition(Node head, int x) 
    /* Let us initialize start and tail nodes of 
    new list */
    Node tail = head; 
  
    // Now iterate original list and connect nodes 
    Node curr = head; 
    while (curr != null
    
        Node next = curr.next; 
        if (curr.data < x) 
        
            /* Insert node at head. */
            curr.next = head; 
            head = curr; 
        
  
        else // Append to the list of greater values 
        
            /* Insert node at tail. */
            tail.next = curr; 
            tail = curr; 
        
        curr = next; 
    
    tail.next = null
  
    // The head has changed, so we need 
    // to return it to the user. 
    return head; 
  
/* Function to print linked list */
static void printList(Node head) 
    Node temp = head; 
    while (temp != null
    
        System.out.print(temp.data + " "); 
        temp = temp.next; 
    
  
// Driver code 
public static void main(String[] args) 
    /* Start with the empty list */
    Node head = newNode(3); 
    head.next = newNode(5); 
    head.next.next = newNode(8); 
    head.next.next.next = newNode(2); 
    head.next.next.next.next = newNode(10); 
    head.next.next.next.next.next = newNode(2); 
    head.next.next.next.next.next.next = newNode(1); 
  
    int x = 5
    head = partition(head, x); 
    printList(head); 
}
  
// This code is contributed by prerna saini

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to partition a linked list 
// around a given value.
using System;
  
class GfG 
  
/* Link list Node */
class Node 
    public int data; 
    public Node next; 
}
  
// A utility function to create a new node 
static Node newNode(int data) 
    Node new_node = new Node(); 
    new_node.data = data; 
    new_node.next = null
    return new_node; 
  
// Function to make a new list 
// (using the existing nodes) and 
// return head of new list. 
static Node partition(Node head, int x) 
    /* Let us initialize start and  
    tail nodes of new list */
    Node tail = head; 
  
    // Now iterate original list
    // and connect nodes 
    Node curr = head; 
    while (curr != null
    
        Node next = curr.next; 
        if (curr.data < x) 
        
            /* Insert node at head. */
            curr.next = head; 
            head = curr; 
        
  
        else // Append to the list of greater values 
        
            /* Insert node at tail. */
            tail.next = curr; 
            tail = curr; 
        
        curr = next; 
    
    tail.next = null
  
    // The head has changed, so we need 
    // to return it to the user. 
    return head; 
  
/* Function to print linked list */
static void printList(Node head) 
    Node temp = head; 
    while (temp != null
    
        Console.Write(temp.data + " "); 
        temp = temp.next; 
    
  
// Driver code 
public static void Main(String[] args) 
    /* Start with the empty list */
    Node head = newNode(3); 
    head.next = newNode(5); 
    head.next.next = newNode(8); 
    head.next.next.next = newNode(2); 
    head.next.next.next.next = newNode(10); 
    head.next.next.next.next.next = newNode(2); 
    head.next.next.next.next.next.next = newNode(1); 
  
    int x = 5; 
    head = partition(head, x); 
    printList(head); 
}
}
  
// This code has been contributed by Rajput-Ji 

chevron_right



Output:

1  2  2  3  5  8  10  

This article is contributed by Mr. Somesh Awasthi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : prerna saini, Rajput-Ji



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.