`{"id":"bc9e105c-b8d1-44f7-9bbb-026bbc020bd0","name":"Segregate Even And Odd Nodes In A Linkedlist","description":"Given a singly linklist, modify the list such that all the even numbers appear before all the odd numbers in the modified list. The order of appearance of numbers within each segregation should be same as that in the original list.\r\n","inputFormat":"1->7->2->6->3->5->4->null\r\n","outputFormat":"2->6->4->1->7->3->5->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 *segregateEvenOdd(ListNode *head)\r\n{\r\n return nullptr;\r\n}\r\n\r\nint main()\r\n{\r\n int n;\r\n cin >> 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\r\n ListNode *head = segregateEvenOdd(dummy->next);\r\n while (head != nullptr)\r\n {\r\n cout << head->val << \" \";\r\n head = head->next;\r\n }\r\n return 0;\r\n}"},"java":{"code":"import java.util.*;\r\n\r\nclass Main {\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 segregateEvenOdd(ListNode head) {\r\n return null;\r\n }\r\n\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\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 ListNode head = segregateEvenOdd(dummy.next);\r\n while (head != null) {\r\n System.out.print(head.val + \" \");\r\n head = head.next;\r\n }\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"4\r\n0\r\n6\r\n7\r\n5\r\n","sampleOutput":"0 6 7 5 ","questionVideo":"https://www.youtube.com/embed/35EDBiYVEsI","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":"a5814dc7-4dd1-4845-a78b-67c185b05d22","name":"Segregate Even And Odd Nodes In A Linkedlist","slug":"segregate-even-and-odd-nodes-in-a-linkedlist","type":1}],"next":{"id":"fa0fabeb-6032-4f1a-8f3b-826b1bff07c1","name":"Segregate Even And Odd Nodes In A LinkedList","type":3,"slug":"segregate-even-and-odd-nodes-in-a-linkedlist"},"prev":{"id":"fbd434de-8925-47f2-945e-a0969a55b702","name":"Merge K Sorted LinkedList","type":3,"slug":"merge-k-sorted-linkedlist"}}}`

# Segregate Even And Odd Nodes In A Linkedlist

Given a singly linklist, modify the list such that all the even numbers appear before all the odd numbers in the modified list. The order of appearance of numbers within each segregation should be same as that in the original list.

`{"id":"bc9e105c-b8d1-44f7-9bbb-026bbc020bd0","name":"Segregate Even And Odd Nodes In A Linkedlist","description":"Given a singly linklist, modify the list such that all the even numbers appear before all the odd numbers in the modified list. The order of appearance of numbers within each segregation should be same as that in the original list.\r\n","inputFormat":"1->7->2->6->3->5->4->null\r\n","outputFormat":"2->6->4->1->7->3->5->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 *segregateEvenOdd(ListNode *head)\r\n{\r\n return nullptr;\r\n}\r\n\r\nint main()\r\n{\r\n int n;\r\n cin >> 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\r\n ListNode *head = segregateEvenOdd(dummy->next);\r\n while (head != nullptr)\r\n {\r\n cout << head->val << \" \";\r\n head = head->next;\r\n }\r\n return 0;\r\n}"},"java":{"code":"import java.util.*;\r\n\r\nclass Main {\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 segregateEvenOdd(ListNode head) {\r\n return null;\r\n }\r\n\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\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 ListNode head = segregateEvenOdd(dummy.next);\r\n while (head != null) {\r\n System.out.print(head.val + \" \");\r\n head = head.next;\r\n }\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"4\r\n0\r\n6\r\n7\r\n5\r\n","sampleOutput":"0 6 7 5 ","questionVideo":"https://www.youtube.com/embed/35EDBiYVEsI","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":"a5814dc7-4dd1-4845-a78b-67c185b05d22","name":"Segregate Even And Odd Nodes In A Linkedlist","slug":"segregate-even-and-odd-nodes-in-a-linkedlist","type":1}],"next":{"id":"fa0fabeb-6032-4f1a-8f3b-826b1bff07c1","name":"Segregate Even And Odd Nodes In A LinkedList","type":3,"slug":"segregate-even-and-odd-nodes-in-a-linkedlist"},"prev":{"id":"fbd434de-8925-47f2-945e-a0969a55b702","name":"Merge K Sorted LinkedList","type":3,"slug":"merge-k-sorted-linkedlist"}}}`

Editor

# Segregate Even And Odd Nodes In A Linkedlist

easy

Given a singly linklist, modify the list such that all the even numbers appear before all the odd numbers in the modified list. The order of appearance of numbers within each segregation should be same as that in the original list.

0 <= N <= 10^6

## Format

### Input

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

### Output

2->6->4->1->7->3->5->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 6 7 5 `

Question Video

Discussions

Show Discussion

Related Resources