{"id":"35e3fed2-2222-45d0-ab1c-eccb896affdb","name":"Segregate 012 Node Of Linkedlist By Swapping Data","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\n3. You are only allowed to swap data not swap 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}"},"ruby":{"code":""},"python":{"code":""},"javascript":{"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":"","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":"a28cc7f1-1eee-4c6b-bf3e-556f5d58866d","name":"Segregate 012 Node Of Linkedlist By Swapping Data","slug":"segregate-012-node-of-linkedlist-by-swapping-data","type":1}],"next":{"id":"41d10ecc-3c86-4912-9d9b-33a1c65f5529","name":"Segregate 012 Node Of Linkedlist Over Swapping Nodes MCQ","type":0,"slug":"segregate-012-node-of-linkedlist-over-swapping-nodes-mcq"},"prev":{"id":"43fd6a2a-4c3f-4c23-9510-ed0a19ba87ec","name":"Segregate 01 Node Of Linkedlist Over Swapping Nodes","type":3,"slug":"segregate-01-node-of-linkedlist-over-swapping-nodes"}}}

Segregate 012 Node Of Linkedlist By Swapping Data

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. 3. You are only allowed to swap data not swap nodes.

{"id":"35e3fed2-2222-45d0-ab1c-eccb896affdb","name":"Segregate 012 Node Of Linkedlist By Swapping Data","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\n3. You are only allowed to swap data not swap 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}"},"ruby":{"code":""},"python":{"code":""},"javascript":{"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":"","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":"a28cc7f1-1eee-4c6b-bf3e-556f5d58866d","name":"Segregate 012 Node Of Linkedlist By Swapping Data","slug":"segregate-012-node-of-linkedlist-by-swapping-data","type":1}],"next":{"id":"41d10ecc-3c86-4912-9d9b-33a1c65f5529","name":"Segregate 012 Node Of Linkedlist Over Swapping Nodes MCQ","type":0,"slug":"segregate-012-node-of-linkedlist-over-swapping-nodes-mcq"},"prev":{"id":"43fd6a2a-4c3f-4c23-9510-ed0a19ba87ec","name":"Segregate 01 Node Of Linkedlist Over Swapping Nodes","type":3,"slug":"segregate-01-node-of-linkedlist-over-swapping-nodes"}}}
plane

Editor


Loading...

Segregate 012 Node Of Linkedlist By Swapping Data

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. 3. You are only allowed to swap data not swap 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

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode