Quick Sort on Linked List



Sort the given Linked List using quicksort. which takes O(n^2) time in worst case and O(nLogn) in average and best cases, otherwise you may get TLE.

Input:
In this problem, method takes 1 argument: address of the head of the linked list. The function should not read any input from stdin/console.
The struct Node has a data part which stores the data and a next pointer which points to the next element of the linked list.
There are multiple test cases. For each test case, this method will be called individually.

Output:
Set *headRef to head of resultant linked list.

User Task:
The task is to complete the function quickSort() which should set the *headRef to head of the resultant linked list.

Constraints:
1<=T<=100
1<=N<=200

Note: If you use "Test" or "Expected Output Button" use below example format




#User function Template for python3


def quickSort(head):

    a=[]

    while head:

        a.append(head.data)

        head=head.next

    p=Quick(a) #Calling QuickSort in


    for i in p:

        print(i,end=" ")

def Quick(a):

    if len(a)<=1:

        return a

    else:

        pivot=a.pop()

        left=[]

        right=[]

        for x in a:

           if x<pivot: 

               left.append(x)

           else: 

               right.append(x)

    return Quick(left)+[pivot]+Quick(right)

    

Comments

Popular posts from this blog

Large Factorial of array

Value equal to index value

Largest Element in Array