`{"id":"4b6d5006-c6b6-4c93-8452-93275afd28f8","name":"Quicksort In Linkedlist","description":"1. Given the head of a linked list, return the list after sorting it in increasing order.\r\n2. You must apply quick sort.\r\n3. Time Complexity : O(nlogn)\r\n4. Space Complexity : constant space \r\n","inputFormat":"1->7->2->6->3->5->4->null\r\n","outputFormat":"1->2->3->4->5->6->7->null\r\n","constraints":"0 &lt;= N &lt;= 10^6\r\n","sampleCode":{"cpp":{"code":"#include <iostream>\r\nusing namespace std;\r\n\r\nclass ListNode\r\n{\r\npublic:\r\n int val = 0;\r\n ListNode *next = nullptr;\r\n\r\n ListNode(int val)\r\n {\r\n this->val = val;\r\n }\r\n};\r\n\r\nListNode *quickSort(ListNode *head)\r\n{\r\n return nullptr;\r\n}\r\n\r\nvoid printList(ListNode *node)\r\n{\r\n ListNode *curr = node;\r\n while (curr != nullptr)\r\n {\r\n cout << curr->val << \" \";\r\n curr = curr->next;\r\n }\r\n cout << endl;\r\n}\r\n\r\nListNode *createList(int n)\r\n{\r\n ListNode *dummy = new ListNode(-1);\r\n ListNode *prev = dummy;\r\n while (n-- > 0)\r\n {\r\n int val;\r\n cin >> val;\r\n prev->next = new ListNode(val);\r\n prev = prev->next;\r\n }\r\n return dummy->next\r\n}\r\n\r\nint main()\r\n{\r\n int n;\r\n cin >> n;\r\n ListNode *h1 = createList(n);\r\n\r\n ListNode *head = quickSort(h1);\r\n printList(head);\r\n\r\n return 0;\r\n}"},"java":{"code":"import java.util.*;\r\n\r\nclass Main {\r\n public static Scanner scn = new Scanner(System.in);\r\n\r\n public static class ListNode {\r\n int val = 0;\r\n ListNode next = null;\r\n\r\n ListNode(int val) {\r\n this.val = val;\r\n }\r\n }\r\n\r\n public static ListNode quickSort(ListNode head) {\r\n return null;\r\n }\r\n\r\n public static void printList(ListNode node) {\r\n while (node != null) {\r\n System.out.print(node.val + \" \");\r\n node = node.next;\r\n }\r\n }\r\n\r\n public static ListNode createList(int n) {\r\n ListNode dummy = new ListNode(-1);\r\n ListNode prev = dummy;\r\n while (n-- > 0) {\r\n prev.next = new ListNode(scn.nextInt());\r\n prev = prev.next;\r\n }\r\n\r\n return dummy.next;\r\n }\r\n\r\n public static void main(String[] args) {\r\n int n = scn.nextInt();\r\n ListNode h1 = createList(n);\r\n\r\n ListNode head = quickSort(h1);\r\n printList(head);\r\n }\r\n}"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"4\r\n0\r\n6\r\n7\r\n5\r\n","sampleOutput":"0 5 6 7 ","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"1e4c8949-5890-4d15-be5b-6601c7e2029a","name":"Linked List For Intermediate","slug":"linked-list-for-intermediate-637","type":0},{"id":"a2fca537-b4c0-4f64-8db5-53826b547863","name":"Quicksort In Linkedlist","slug":"quicksort-in-linkedlist","type":1}],"next":{"id":"c5fffeb5-1963-425e-881f-3c1d2d57a054","name":"Add First In Doubly Linkedlist","type":1,"slug":"add-first-in-doubly-linkedlist"},"prev":{"id":"009f1856-0e8c-425e-b752-bb981d5371e4","name":"Segregate Node Of Linkedlist Over Pivot Index","type":1,"slug":"segregate-node-of-linkedlist-over-pivot-index"}}}`

1. Given the head of a linked list, return the list after sorting it in increasing order. 2. You must apply quick sort. 3. Time Complexity : O(nlogn) 4. Space Complexity : constant space

`{"id":"4b6d5006-c6b6-4c93-8452-93275afd28f8","name":"Quicksort In Linkedlist","description":"1. Given the head of a linked list, return the list after sorting it in increasing order.\r\n2. You must apply quick sort.\r\n3. Time Complexity : O(nlogn)\r\n4. Space Complexity : constant space \r\n","inputFormat":"1->7->2->6->3->5->4->null\r\n","outputFormat":"1->2->3->4->5->6->7->null\r\n","constraints":"0 &lt;= N &lt;= 10^6\r\n","sampleCode":{"cpp":{"code":"#include <iostream>\r\nusing namespace std;\r\n\r\nclass ListNode\r\n{\r\npublic:\r\n int val = 0;\r\n ListNode *next = nullptr;\r\n\r\n ListNode(int val)\r\n {\r\n this->val = val;\r\n }\r\n};\r\n\r\nListNode *quickSort(ListNode *head)\r\n{\r\n return nullptr;\r\n}\r\n\r\nvoid printList(ListNode *node)\r\n{\r\n ListNode *curr = node;\r\n while (curr != nullptr)\r\n {\r\n cout << curr->val << \" \";\r\n curr = curr->next;\r\n }\r\n cout << endl;\r\n}\r\n\r\nListNode *createList(int n)\r\n{\r\n ListNode *dummy = new ListNode(-1);\r\n ListNode *prev = dummy;\r\n while (n-- > 0)\r\n {\r\n int val;\r\n cin >> val;\r\n prev->next = new ListNode(val);\r\n prev = prev->next;\r\n }\r\n return dummy->next\r\n}\r\n\r\nint main()\r\n{\r\n int n;\r\n cin >> n;\r\n ListNode *h1 = createList(n);\r\n\r\n ListNode *head = quickSort(h1);\r\n printList(head);\r\n\r\n return 0;\r\n}"},"java":{"code":"import java.util.*;\r\n\r\nclass Main {\r\n public static Scanner scn = new Scanner(System.in);\r\n\r\n public static class ListNode {\r\n int val = 0;\r\n ListNode next = null;\r\n\r\n ListNode(int val) {\r\n this.val = val;\r\n }\r\n }\r\n\r\n public static ListNode quickSort(ListNode head) {\r\n return null;\r\n }\r\n\r\n public static void printList(ListNode node) {\r\n while (node != null) {\r\n System.out.print(node.val + \" \");\r\n node = node.next;\r\n }\r\n }\r\n\r\n public static ListNode createList(int n) {\r\n ListNode dummy = new ListNode(-1);\r\n ListNode prev = dummy;\r\n while (n-- > 0) {\r\n prev.next = new ListNode(scn.nextInt());\r\n prev = prev.next;\r\n }\r\n\r\n return dummy.next;\r\n }\r\n\r\n public static void main(String[] args) {\r\n int n = scn.nextInt();\r\n ListNode h1 = createList(n);\r\n\r\n ListNode head = quickSort(h1);\r\n printList(head);\r\n }\r\n}"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"4\r\n0\r\n6\r\n7\r\n5\r\n","sampleOutput":"0 5 6 7 ","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"1e4c8949-5890-4d15-be5b-6601c7e2029a","name":"Linked List For Intermediate","slug":"linked-list-for-intermediate-637","type":0},{"id":"a2fca537-b4c0-4f64-8db5-53826b547863","name":"Quicksort In Linkedlist","slug":"quicksort-in-linkedlist","type":1}],"next":{"id":"c5fffeb5-1963-425e-881f-3c1d2d57a054","name":"Add First In Doubly Linkedlist","type":1,"slug":"add-first-in-doubly-linkedlist"},"prev":{"id":"009f1856-0e8c-425e-b752-bb981d5371e4","name":"Segregate Node Of Linkedlist Over Pivot Index","type":1,"slug":"segregate-node-of-linkedlist-over-pivot-index"}}}`

Editor

easy

1. Given the head of a linked list, return the list after sorting it in increasing order. 2. You must apply quick sort. 3. Time Complexity : O(nlogn) 4. Space Complexity : constant space

0 <= N <= 10^6

## Format

### Input

1->7->2->6->3->5->4->null

### Output

1->2->3->4->5->6->7->null

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}4 0 6 7 5 ```

### Sample Output

`.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}0 5 6 7 `

Discussions

Show Discussion

Related Resources