QuickSort on Singly Linked List
Last Updated :
23 Jul, 2025
Given a linked list, apply the Quick sort algorithm to sort the linked list. To sort the Linked list change pointers rather than swapping data.
Example:
Input: 5->4->1->3->2
Output: 1->2->3->4->5
Input: 4->3->2->1
Output: 1->2->3->4
Approach:
The Quick Sort algorithm works by selecting a pivot element from the array, typically the last element. The array is then partitioned such that all elements smaller than the pivot are placed on the left, while those greater than the pivot are placed on the right. Once the partitioning is complete, the algorithm recursively applies the same process to the left and right subarrays.
This recursive sorting continues until the entire array is sorted, with the sorted subarrays and pivot being combined to form the final sorted array.
Follow the given steps to solve the problem:
- Call partition function to get a pivot node placed at its correct position.
- In the partition function, the last element is considered a pivot.
- Then traverse the current list and if a node has a value greater than the pivot, then move it after the tail. If the node has a smaller value, then keep it at its current position.
- Return pivot node
- Find the tail node of the list which is on the left side of the pivot and recur for the left list
- Similarly, after the left side, recur for the list on the right side of the pivot
- Now return the head of the linked list after joining the left and the right list, as the whole linked list is now sorted.
Below is the implementation of the above approach:
C++
// C++ program for Quick Sort on Singly Linked List
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int x) {
data = x;
next = nullptr;
}
};
void printList(Node* curr) {
while (curr != nullptr) {
cout << curr->data << " ";
curr = curr->next;
}
cout << endl;
}
// Returns the last node of the list
Node* getTail(Node* cur) {
while (cur != nullptr && cur->next != nullptr)
cur = cur->next;
return cur;
}
// Partitions the list taking the first element as the pivot
Node* partition(Node* head, Node* tail) {
// Select the first node as the pivot node
Node* pivot = head;
// 'pre' and 'curr' are used to shift all
// smaller nodes' data to the left side of the pivot node
Node* pre = head;
Node* curr = head;
// Traverse the list until you reach the node after the tail
while (curr != tail->next) {
if (curr->data < pivot->data) {
swap(curr->data, pre->next->data);
// Move 'pre' to the next node
pre = pre->next;
}
// Move 'curr' to the next node
curr = curr->next;
}
swap(pivot->data, pre->data);
// Return 'pre' as the new pivot
return pre;
}
// Helper function for quick sort
void quickSortHelper(Node* head, Node* tail) {
// Base case: if the list is empty or consists of a single node
if (head == nullptr || head == tail) {
return;
}
// Call partition to find the pivot node
Node* pivot = partition(head, tail);
// Recursive call for the left part of the list (before the pivot)
quickSortHelper(head, pivot);
// Recursive call for the right part of the list (after the pivot)
quickSortHelper(pivot->next, tail);
}
// The main function for quick sort. This is a wrapper over quickSortHelper
Node* quickSort(Node* head) {
Node* tail = getTail(head);
// Call the helper function to sort the list
quickSortHelper(head, tail);
return head;
}
int main() {
// Creating a linked list: 30 -> 3 -> 4 -> 20 -> 5
Node* head = new Node(30);
head->next = new Node(3);
head->next->next = new Node(4);
head->next->next->next = new Node(20);
head->next->next->next->next = new Node(5);
head = quickSort(head);
printList(head);
return 0;
}
C
// C program for Quick Sort on Singly Linked List
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void printList(struct Node* curr) {
while (curr != NULL) {
printf("%d ", curr->data);
curr = curr->next;
}
printf("\n");
}
// Returns the last node of the list
struct Node* getTail(struct Node* cur) {
while (cur != NULL && cur->next != NULL)
cur = cur->next;
return cur;
}
// Partitions the list taking the first element as the pivot
struct Node* partition(struct Node* head,struct Node* tail) {
// Select the first node as the pivot node
struct Node* pivot = head;
// 'pre' and 'curr' are used to shift all
// smaller nodes' data to the left side of the pivot node
struct Node* pre = head;
struct Node* curr = head;
// Traverse the list until you reach the node after the tail
while (curr != tail->next) {
// If current node's data is less than the pivot's data
if (curr->data < pivot->data) {
// Swap the data between 'curr' and 'pre->next'
int temp = curr->data;
curr->data = pre->next->data;
pre->next->data = temp;
// Move 'pre' to the next node
pre = pre->next;
}
// Move 'curr' to the next node
curr = curr->next;
}
// Swap the pivot's data with 'pre' data
int currData = pivot->data;
pivot->data = pre->data;
pre->data = currData;
// Return 'pre' as the new pivot
return pre;
}
// Helper function for quick sort
void quickSortHelper(struct Node* head, struct Node* tail) {
// Base case: if the list is empty or consists of a single node
if (head == NULL || head == tail) {
return;
}
// Call partition to find the pivot node
struct Node* pivot = partition(head, tail);
// Recursive call for the left part
// of the list (before the pivot)
quickSortHelper(head, pivot);
// Recursive call for the right part
// of the list (after the pivot)
quickSortHelper(pivot->next, tail);
}
// The main function for quick sort. This is a
// wrapper over quickSortHelper
struct Node* quickSort(struct Node* head) {
// Find the tail of the list
struct Node* tail = getTail(head);
// Call the helper function to sort the list
quickSortHelper(head, tail);
return head;
}
struct Node* createNode(int x) {
struct Node* newNode =
(struct Node*)malloc(sizeof(struct Node));
newNode->data = x;
newNode->next = NULL;
return newNode;
}
int main() {
// Creating a linked list: 30 -> 3 -> 4 -> 20 -> 5
struct Node* head = createNode(30);
head->next = createNode(3);
head->next->next = createNode(4);
head->next->next->next = createNode(20);
head->next->next->next->next = createNode(5);
head = quickSort(head);
printList(head);
return 0;
}
Java
// Java program for Quick Sort on Singly Linked List
class Node {
int data;
Node next;
Node(int x) {
data = x;
next = null;
}
}
class GfG {
static void printList(Node curr) {
while (curr != null) {
System.out.print(curr.data + " ");
curr = curr.next;
}
System.out.println();
}
// Returns the last node of the list
static Node getTail(Node cur) {
while (cur != null && cur.next != null)
cur = cur.next;
return cur;
}
// Partitions the list taking the first element as the pivot
static Node partition(Node head, Node tail) {
// Select the first node as the pivot node
Node pivot = head;
// 'pre' and 'curr' are used to shift all
// smaller nodes' data to the left side of the pivot node
Node pre = head;
Node curr = head;
// Traverse the list until you reach the node after the tail
while (curr != tail.next) {
// If current node's data is less than the pivot's data
if (curr.data < pivot.data) {
int temp = curr.data;
curr.data = pre.next.data;
pre.next.data = temp;
// Move 'pre' to the next node
pre = pre.next;
}
// Move 'curr' to the next node
curr = curr.next;
}
// Swap the pivot's data with 'pre' data
int currData = pivot.data;
pivot.data = pre.data;
pre.data = currData;
// Return 'pre' as the new pivot
return pre;
}
// Helper function for quick sort
static void quickSortHelper(Node head, Node tail) {
// Base case: if the list is empty or consists of a single node
if (head == null || head == tail) {
return;
}
// Call partition to find the pivot node
Node pivot = partition(head, tail);
// Recursive call for the left part of
// the list (before the pivot)
quickSortHelper(head, pivot);
// Recursive call for the right part of
// the list (after the pivot)
quickSortHelper(pivot.next, tail);
}
// The main function for quick sort.
// This is a wrapper over quickSortHelper
static Node quickSort(Node head) {
// Find the tail of the list
Node tail = getTail(head);
// Call the helper function to sort the list
quickSortHelper(head, tail);
return head;
}
public static void main(String[] args) {
// Creating a linked list: 30 -> 3 -> 4 -> 20 -> 5
Node head = new Node(30);
head.next = new Node(3);
head.next.next = new Node(4);
head.next.next.next = new Node(20);
head.next.next.next.next = new Node(5);
head = quickSort(head);
printList(head);
}
}
Python
# Python program for Quick Sort on Singly Linked List
class Node:
def __init__(self, x):
self.data = x
self.next = None
def printList(curr):
while curr:
print(curr.data, end=" ")
curr = curr.next
print()
# Returns the last node of the list
def getTail(cur):
while cur and cur.next:
cur = cur.next
return cur
# Partitions the list taking the first element as the pivot
def partition(head, tail):
# Select the first node as the pivot node
pivot = head
# 'pre' and 'curr' are used to shift all
# smaller nodes' data to the left side of the pivot node
pre = head
curr = head
# Traverse the list until you reach the node after the tail
while curr != tail.next:
# If current node's data is less than the pivot's data
if curr.data < pivot.data:
# Swap the data between 'curr' and 'pre.next'
curr.data, pre.next.data = pre.next.data, curr.data
pre = pre.next
curr = curr.next
# Swap the pivot's data with 'pre' data
pivot.data, pre.data = pre.data, pivot.data
# Return 'pre' as the new pivot
return pre
def quickSortHelper(head, tail):
# Base case: if the list is empty or consists of a single node
if head is None or head == tail:
return
# Call partition to find the pivot node
pivot = partition(head, tail)
# Recursive call for the left part of the list (before the pivot)
quickSortHelper(head, pivot)
# Recursive call for the right part of the list (after the pivot)
quickSortHelper(pivot.next, tail)
# The main function for quick sort.
# This is a wrapper over quickSortHelper
def quickSort(head):
# Find the tail of the list
tail = getTail(head)
# Call the helper function to sort the list
quickSortHelper(head, tail)
return head
if __name__ == "__main__":
# Creating a linked list: 30 -> 3 -> 4 -> 20 -> 5
head = Node(30)
head.next = Node(3)
head.next.next = Node(4)
head.next.next.next = Node(20)
head.next.next.next.next = Node(5)
head = quickSort(head)
printList(head)
C#
// C# program for Quick Sort on
// Singly Linked List
using System;
class Node {
public int Data;
public Node next;
public Node(int x) {
Data = x;
next = null;
}
}
class GfG {
static void PrintList(Node curr) {
while (curr != null) {
Console.Write(curr.Data + " ");
curr = curr.next;
}
Console.WriteLine();
}
// Returns the last node of the list
static Node GetTail(Node cur) {
while (cur != null && cur.next != null)
cur = cur.next;
return cur;
}
// Partitions the list taking the first element as the pivot
static Node Partition(Node head, Node tail) {
// Select the first node as the pivot node
Node pivot = head;
// 'pre' and 'curr' are used to shift all
// smaller nodes' data to the left side of the pivot node
Node pre = head;
Node curr = head;
// Traverse the list until you reach the node after the tail
while (curr != tail.next) {
// If current node's data is less than the pivot's data
if (curr.Data < pivot.Data) {
// Swap the data between 'curr' and 'pre->Next'
int temp = curr.Data;
curr.Data = pre.next.Data;
pre.next.Data = temp;
// Move 'pre' to the next node
pre = pre.next;
}
// Move 'curr' to the next node
curr = curr.next;
}
// Swap the pivot's data with 'pre' data
int currData = pivot.Data;
pivot.Data = pre.Data;
pre.Data = currData;
// Return 'pre' as the new pivot
return pre;
}
// Helper function for quick sort
static void QuickSortHelper(Node head, Node tail) {
// Base case: if the list is empty or consists of a single node
if (head == null || head == tail) {
return;
}
// Call partition to find the pivot node
Node pivot = Partition(head, tail);
// Recursive call for the left
// part of the list (before the pivot)
QuickSortHelper(head, pivot);
// Recursive call for the right part
// of the list (after the pivot)
QuickSortHelper(pivot.next, tail);
}
// The main function for quick sort.
// This is a wrapper over QuickSortHelper
static Node QuickSort(Node head) {
// Find the tail of the list
Node tail = GetTail(head);
// Call the helper function to sort the list
QuickSortHelper(head, tail);
return head;
}
static void Main() {
// Creating a linked list: 30 -> 3 -> 4 -> 20 -> 5
Node head = new Node(30);
head.next = new Node(3);
head.next.next = new Node(4);
head.next.next.next = new Node(20);
head.next.next.next.next = new Node(5);
head = QuickSort(head);
PrintList(head);
}
}
JavaScript
// Javascript program for Quick Sort on Singly Linked List
class Node {
constructor(x) {
this.data = x;
this.next = null;
}
}
function printList(curr) {
while (curr) {
console.log(curr.data + " ");
curr = curr.next;
}
console.log();
}
// Returns the last node of the list
function getTail(cur) {
while (cur && cur.next) {
cur = cur.next;
}
return cur;
}
// Partitions the list taking the first element as the pivot
function partition(head, tail) {
// Select the first node as the pivot node
let pivot = head;
// 'pre' and 'curr' are used to shift all
// smaller nodes' data to the left side of the pivot node
let pre = head;
let curr = head;
// Traverse the list until you reach the node after the tail
while (curr !== tail.next) {
// If current node's data is less than the pivot's data
if (curr.data < pivot.data) {
// Swap the data between 'curr' and 'pre.next'
[curr.data, pre.next.data] = [pre.next.data, curr.data];
// Move 'pre' to the next node
pre = pre.next;
}
// Move 'curr' to the next node
curr = curr.next;
}
// Swap the pivot's data with 'pre' data
[pivot.data, pre.data] = [pre.data, pivot.data];
// Return 'pre' as the new pivot
return pre;
}
// Helper function for quick sort
function quickSortHelper(head, tail) {
// Base case: if the list is empty or
// consists of a single node
if (!head || head === tail) {
return;
}
// Call partition to find the pivot node
let pivot = partition(head, tail);
// Recursive call for the left part
// of the list (before the pivot)
quickSortHelper(head, pivot);
// Recursive call for the right part
// of the list (after the pivot)
quickSortHelper(pivot.next, tail);
}
// The main function for quick sort.
// This is a wrapper over quickSortHelper
function quickSort(head) {
// Find the tail of the list
let tail = getTail(head);
// Call the helper function to sort the list
quickSortHelper(head, tail);
return head;
}
// Creating a linked list: 30 -> 3 -> 4 -> 20 -> 5
let head = new Node(30);
head.next = new Node(3);
head.next.next = new Node(4);
head.next.next.next = new Node(20);
head.next.next.next.next = new Node(5);
head = quickSort(head);
printList(head);
Time Complexity: O(n * log n), It takes O(n2) time in the worst case and O(n log n) in the average or best case.
Auxiliary Space: O(n)
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem