diff --git a/algorithms/CPlusPlus/Linked-Lists/segregate-even-odd-nodes-of-linked-list.cpp b/algorithms/CPlusPlus/Linked-Lists/segregate-even-odd-nodes-of-linked-list.cpp new file mode 100644 index 00000000..c5ca8298 --- /dev/null +++ b/algorithms/CPlusPlus/Linked-Lists/segregate-even-odd-nodes-of-linked-list.cpp @@ -0,0 +1,99 @@ +/* +The idea is to split the linked list into two: +one containing all even nodes and other containing all odd nodes. +And finally, attach the odd node linked list after the even node linked list. +To split the Linked List, traverse the original Linked List and move all odd nodes to a separate Linked List of all odd nodes. +At the end of loop, the original list will have all the even nodes and the odd node list will have all the odd nodes. +To keep the ordering of all nodes same, we must insert all the odd nodes at the end of the even node list. +And to do that in constant time, we must keep track of last pointer in the even node list +*/ + +#include +using namespace std; + +/* a node of the singly linked list */ +struct Node{ + int data; + Node* next; + Node(int x){ + data=x; + next=NULL; + } +}; + +/* Function to print nodes in a given linked list */ +void printlist(Node *head){ + Node *curr=head; + while(curr!=NULL){ + cout<data<<" "; + curr=curr->next; + }cout<next){ + int x=curr->data; + // If current value is even, add + // it to even values list. + if(x%2==0){ + if(eS==NULL){ + eS=curr; + eE=eS; + }else{ + eE->next=curr; + eE=eE->next; + } + } + // If current value is odd, add + // it to odd values list. + else{ + if(oS==NULL){ + oS=curr; + oE=oS; + }else{ + oE->next=curr; + oE=oE->next; + } + } + } + // If either odd list or even list is empty, + // no change is required as all elements + // are either even or odd. + if(oS==NULL||eS==NULL) + return head; + // Add odd list after even list. + eE->next=oS; + oE->next=NULL; + return eS; +} + +/* Driver program to test above functions*/ +int main() +{ + Node *head=new Node(10); + head->next=new Node(11); + head->next->next=new Node(12); + head->next->next->next=new Node(13); + head->next->next->next->next=new Node(14); + head->next->next->next->next->next=new Node(15); + printlist(head); + head=segregate(head); + printlist(head); + return 0; +} + +/* +Input: 10 11 12 13 14 15 +Output: 10 12 14 11 13 15 + +Time complexity: O(n) +*/ diff --git a/algorithms/CPlusPlus/README.md b/algorithms/CPlusPlus/README.md index 3d85290a..786f6400 100644 --- a/algorithms/CPlusPlus/README.md +++ b/algorithms/CPlusPlus/README.md @@ -45,6 +45,7 @@ 8. [Reorder List](Linked-Lists/Reorder-List.cpp) 9. [Detecting cycle in a singly linked list](Linked-Lists/Cycle-Detection.cpp) 10. [Find Merge Point of two singly linked list](Linked-Lists/Find-Merge-Point.cpp) +11. [Segregate Even Odd Nodes of linked list](Linked-Lists/segregate-even-odd-nodes-of-linked-list.cpp) ## Searching