Open In App

Java Program For QuickSort On Singly Linked List

Last Updated : 17 Aug, 2023
Like Article

QuickSort on Doubly Linked List is discussed here. QuickSort on Singly linked list was given as an exercise. The important things about implementation are, it changes pointers rather swapping data and time complexity is same as the implementation for Doubly Linked List. 

sorting image

In partition(), we consider last element as pivot. We traverse through the current list and if a node has value greater than pivot, we move it after tail. If the node has smaller value, we keep it at its current position. 

In QuickSortRecur(), we first call partition() which places pivot at correct position and returns pivot. After pivot is placed at correct position, we find tail node of left side (list before pivot) and recur for left list. Finally, we recur for right list.


// Java program for Quick Sort on 
// Singly Linked List
// Sort a linked list using 
// quick sort
public class QuickSortLinkedList 
    static class Node 
        int data;
        Node next;
        Node(int d)
   = d;
   = null;
    Node head;
    void addNode(int data)
        if (head == null
            head = new Node(data);
        Node curr = head;
        while ( != null)
            curr =;
        Node newNode = new Node(data); = newNode;
    void printList(Node n)
        while (n != null
            System.out.print(" ");
            n =;
    // Takes first and last node,
    // but do not break any links in
    // the whole linked list
    Node partitionLast(Node start, 
                      Node end)
        if (start == end || 
            start == null || 
            end == null)
            return start;
        Node pivot_prev = start;
        Node curr = start;
        int pivot =;
        // Iterate till one before the end,
        // no need to iterate till the end
        // because end is pivot
        while (start != end) 
            if ( < pivot) 
                // Keep tracks of last 
                // modified item
                pivot_prev = curr;
                int temp =;
       = temp;
                curr =;
            start =;
        // Swap the position of curr i.e.
        // next suitable index and pivot
        int temp =; = pivot; = temp;
        // Return one previous to current
        // because current is now pointing 
        // to pivot
        return pivot_prev;
    void sort(Node start, Node end)
        if(start == null || 
           start == end|| 
           start == )
        // Split list and partition recurse
        Node pivot_prev = partitionLast(start, end);
        sort(start, pivot_prev);
        // If pivot is picked and moved to the start,
        // that means start and pivot is same
        // so pick from next of pivot
        if (pivot_prev != null && 
            pivot_prev == start)
            sort(, end);
        // If pivot is in between of the list,
        // start from next of pivot, since we 
        // have pivot_prev, so we move two nodes
        else if (pivot_prev != null && 
        != null)
            sort(, end);
    // Driver Code
    public static void main(String[] args)
        QuickSortLinkedList list = 
                 new QuickSortLinkedList();
        Node n = list.head;
        while ( != null)
            n =;
               "Linked List before sorting");
        list.sort(list.head, n);
               "Linked List after sorting");
// This code is contributed by trinadumca


Linked List before sorting 
30 3 4 20 5 
Linked List after sorting 
3 4 5 20 30 

Please refer complete article on QuickSort on Singly Linked List for more details!

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads