`{"id":"50284bc8-b254-470e-b2a2-3b3849c8d93e","name":"Segregate Node Of Linkedlist Over Pivot Index","description":"1. Given a singly linklist, Segregate Node of LinkedList over pivot index and return starting node of linkedlist.\r\n2. pivot will be any random index in range of 0 to length Of Linkedlist\r\n3. After segregation pivot Element should have to be present at correct position as in sorted linkedlist.\r\n","inputFormat":"1->5->2->9->5->14->11->1->10->10->1->3->null\r\n11\r\n","outputFormat":"1->2->1->1->3->5->9->5->14->11->10->10->null \r\n","constraints":"-10^6 &lt;= size Of LinkedList &lt;= 10^6\r\n0 &lt;= pivot Index &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* segregate(ListNode* head, int pivotIdx)\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, pivotIdx;\r\n cin >> n;\r\n ListNode* h1 = createList(n);\r\n cin >> pivotIdx;\r\n h1 = segregate(h1, pivotIdx);\r\n printList(h1);\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 segregate(ListNode head, int pivotIdx) {\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 int idx = scn.nextInt();\r\n h1 = segregate(h1, idx);\r\n printList(h1);\r\n }\r\n}"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"12\r\n1 5 2 9 5 14 11 1 10 10 1 3 \r\n7\r\n","sampleOutput":"1 1 1 5 2 9 5 14 11 10 10 3 ","questionVideo":"https://www.youtube.com/embed/sJhUy4ixASE","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":"009f1856-0e8c-425e-b752-bb981d5371e4","name":"Segregate Node Of Linkedlist Over Pivot Index","slug":"segregate-node-of-linkedlist-over-pivot-index","type":1}],"next":{"id":"a2fca537-b4c0-4f64-8db5-53826b547863","name":"Quicksort In Linkedlist","type":1,"slug":"quicksort-in-linkedlist"},"prev":{"id":"0465fd3b-1ce6-4981-8358-bbf32ed4a398","name":"Segregate Node Of Linkedlist Over Last Index.","type":1,"slug":"segregate-node-of-linkedlist-over-last-index"}}}`

# Segregate Node Of Linkedlist Over Pivot Index

1. Given a singly linklist, Segregate Node of LinkedList over pivot index and return starting node of linkedlist. 2. pivot will be any random index in range of 0 to length Of Linkedlist 3. After segregation pivot Element should have to be present at correct position as in sorted linkedlist.

`{"id":"50284bc8-b254-470e-b2a2-3b3849c8d93e","name":"Segregate Node Of Linkedlist Over Pivot Index","description":"1. Given a singly linklist, Segregate Node of LinkedList over pivot index and return starting node of linkedlist.\r\n2. pivot will be any random index in range of 0 to length Of Linkedlist\r\n3. After segregation pivot Element should have to be present at correct position as in sorted linkedlist.\r\n","inputFormat":"1->5->2->9->5->14->11->1->10->10->1->3->null\r\n11\r\n","outputFormat":"1->2->1->1->3->5->9->5->14->11->10->10->null \r\n","constraints":"-10^6 &lt;= size Of LinkedList &lt;= 10^6\r\n0 &lt;= pivot Index &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* segregate(ListNode* head, int pivotIdx)\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, pivotIdx;\r\n cin >> n;\r\n ListNode* h1 = createList(n);\r\n cin >> pivotIdx;\r\n h1 = segregate(h1, pivotIdx);\r\n printList(h1);\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 segregate(ListNode head, int pivotIdx) {\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 int idx = scn.nextInt();\r\n h1 = segregate(h1, idx);\r\n printList(h1);\r\n }\r\n}"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"12\r\n1 5 2 9 5 14 11 1 10 10 1 3 \r\n7\r\n","sampleOutput":"1 1 1 5 2 9 5 14 11 10 10 3 ","questionVideo":"https://www.youtube.com/embed/sJhUy4ixASE","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":"009f1856-0e8c-425e-b752-bb981d5371e4","name":"Segregate Node Of Linkedlist Over Pivot Index","slug":"segregate-node-of-linkedlist-over-pivot-index","type":1}],"next":{"id":"a2fca537-b4c0-4f64-8db5-53826b547863","name":"Quicksort In Linkedlist","type":1,"slug":"quicksort-in-linkedlist"},"prev":{"id":"0465fd3b-1ce6-4981-8358-bbf32ed4a398","name":"Segregate Node Of Linkedlist Over Last Index.","type":1,"slug":"segregate-node-of-linkedlist-over-last-index"}}}`

Editor

# Segregate Node Of Linkedlist Over Pivot Index

medium

1. Given a singly linklist, Segregate Node of LinkedList over pivot index and return starting node of linkedlist. 2. pivot will be any random index in range of 0 to length Of Linkedlist 3. After segregation pivot Element should have to be present at correct position as in sorted linkedlist.

## Constraints

-10^6 <= size Of LinkedList <= 10^6 0 <= pivot Index <= 10^6

## Format

### Input

1->5->2->9->5->14->11->1->10->10->1->3->null 11

### Output

1->2->1->1->3->5->9->5->14->11->10->10->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;}12 1 5 2 9 5 14 11 1 10 10 1 3 7 ```

### 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;}1 1 1 5 2 9 5 14 11 10 10 3 `

Question Video

Discussions

Show Discussion

Related Resources