{"id":"ca505a72-dcdb-4d46-94b5-b5555d18dd8c","name":"Segregate 012 Node Of Linkedlist Over Swapping Nodes","description":"1. Given a singly linklist, Segregate 012 Node of LinkedList and return pivot node of linkedlist.\r\n2. After segregation zero nodes should come first and then ones node followed by two's nodes.\r\n","inputFormat":"1->0->1->0->0->1->2->1->1->1->2->1->1->null\r\n","outputFormat":"0->0->0->1->1->1->1->1->1->1->1->2->2->null\r\n","constraints":"-10^6 &lt;= size Of LinkedList &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 *segregate012(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 h1 = segregate012(h1);\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 segregate012(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 h1 = segregate012(h1);\r\n printList(h1);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"17\r\n2 2 0 2 1 0 0 2 2 1 2 1 2 0 1 0 0 ","sampleOutput":"0 0 0 0 0 0 1 1 1 1 2 2 2 2 2 2 2 ","questionVideo":"https://www.youtube.com/embed/VLhnTzNuntQ","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":"0b172d26-1771-4864-b95d-8c631ea83455","name":"Segregate 01 Node Of Linkedlist Over Swapping nodes MCQ","slug":"segregate-01-node-of-linkedlist-over-swapping-nodes-mcq","type":0},{"id":"6cb17dfa-e9dd-4063-962e-26d504d6c6e7","name":"Segregate 012 Node Of Linkedlist Over Swapping Nodes","slug":"segregate-012-node-of-linkedlist-over-swapping-nodes","type":1}],"next":null,"prev":{"id":"270a4225-2385-464a-ac6b-0641155f70ba","name":"Segregate 01 Node Of Linkedlist Over Swapping Nodes","type":1,"slug":"segregate-01-node-of-linkedlist-over-swapping-nodes"}}}

Segregate 012 Node Of Linkedlist Over Swapping Nodes

1. Given a singly linklist, Segregate 012 Node of LinkedList and return pivot node of linkedlist. 2. After segregation zero nodes should come first and then ones node followed by two's nodes.

{"id":"ca505a72-dcdb-4d46-94b5-b5555d18dd8c","name":"Segregate 012 Node Of Linkedlist Over Swapping Nodes","description":"1. Given a singly linklist, Segregate 012 Node of LinkedList and return pivot node of linkedlist.\r\n2. After segregation zero nodes should come first and then ones node followed by two's nodes.\r\n","inputFormat":"1->0->1->0->0->1->2->1->1->1->2->1->1->null\r\n","outputFormat":"0->0->0->1->1->1->1->1->1->1->1->2->2->null\r\n","constraints":"-10^6 &lt;= size Of LinkedList &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 *segregate012(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 h1 = segregate012(h1);\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 segregate012(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 h1 = segregate012(h1);\r\n printList(h1);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"17\r\n2 2 0 2 1 0 0 2 2 1 2 1 2 0 1 0 0 ","sampleOutput":"0 0 0 0 0 0 1 1 1 1 2 2 2 2 2 2 2 ","questionVideo":"https://www.youtube.com/embed/VLhnTzNuntQ","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":"0b172d26-1771-4864-b95d-8c631ea83455","name":"Segregate 01 Node Of Linkedlist Over Swapping nodes MCQ","slug":"segregate-01-node-of-linkedlist-over-swapping-nodes-mcq","type":0},{"id":"6cb17dfa-e9dd-4063-962e-26d504d6c6e7","name":"Segregate 012 Node Of Linkedlist Over Swapping Nodes","slug":"segregate-012-node-of-linkedlist-over-swapping-nodes","type":1}],"next":null,"prev":{"id":"270a4225-2385-464a-ac6b-0641155f70ba","name":"Segregate 01 Node Of Linkedlist Over Swapping Nodes","type":1,"slug":"segregate-01-node-of-linkedlist-over-swapping-nodes"}}}
plane

Editor


Loading...

Segregate 012 Node Of Linkedlist Over Swapping Nodes

easy

1. Given a singly linklist, Segregate 012 Node of LinkedList and return pivot node of linkedlist. 2. After segregation zero nodes should come first and then ones node followed by two's nodes.

Constraints

-10^6 <= size Of LinkedList <= 10^6

Format

Input

1->0->1->0->0->1->2->1->1->1->2->1->1->null

Output

0->0->0->1->1->1->1->1->1->1->1->2->2->null

Example

Sample Input

17 2 2 0 2 1 0 0 2 2 1 2 1 2 0 1 0 0

Sample Output

0 0 0 0 0 0 1 1 1 1 2 2 2 2 2 2 2

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode