# Remove all even parity nodes from a Doubly and Circular Singly Linked List

Given a Doubly linked list and Circular singly linked list containing N nodes, the task is to remove all the nodes from each list which contains elements whose parity is even.

Example:

Input: CLL = 9 -> 11 -> 34 -> 6 -> 13 -> 21
Output: 11 -> 13 -> 21
Explanation:
The circular singly linked list contains :
11 -> 1011, parity = 3
9 -> 1001, parity = 2
34 -> 100010, parity = 2
6 -> 110, parity = 2
13 -> 1101, parity = 3
21 -> 10101, parity = 3
Here, parity for nodes containing 9, 34, and 6 are even.
Hence, these nodes have been deleted.

Input: DLL = 18 <=> 15 <=> 8 <=> 9 <=> 14
Output: 8 <=> 14
Explanation:
18 -> 10010, parity = 2
15 -> 1111, parity = 4
8 -> 1000, parity = 1
9 -> 1001, parity = 2
14 -> 1110, parity = 3
Here, parity for nodes containing 18, 15 and 9 are even.
Hence, these nodes have been deleted.

Approach:
A simple approach is to traverse the nodes of the list one by one and for each node first, find the parity for the value present in the node by iterating through each bit and then finally remove the nodes whose parity is even.

Below is the implementation of the above approach:

 `// C++ implementation to remove all` `// the Even Parity Nodes from a` `// doubly linked list`   `#include `   `using` `namespace` `std;`   `// Node of the doubly linked list` `struct` `Node {` `    ``int` `data;` `    ``Node *prev, *next;` `};`   `// Function to insert a node at the beginning` `// of the Doubly Linked List` `void` `push(Node** head_ref, ``int` `new_data)` `{` `    ``// Allocate the node` `    ``Node* new_node` `        ``= (Node*)``malloc``(``sizeof``(``struct` `Node));`   `    ``// Insert the data` `    ``new_node->data = new_data;`   `    ``// Since we are adding at the beginning,` `    ``// prev is always NULL` `    ``new_node->prev = NULL;`   `    ``// Link the old list off the new node` `    ``new_node->next = (*head_ref);`   `    ``// Change the prev of` `    ``// head node to new node` `    ``if` `((*head_ref) != NULL)` `        ``(*head_ref)->prev = new_node;`   `    ``// Move the head to point` `    ``// to the new node` `    ``(*head_ref) = new_node;` `}`   `// Function that returns true if count` `// of set bits in x is even` `bool` `isEvenParity(``int` `x)` `{` `    ``// parity will store the` `    ``// count of set bits` `    ``int` `parity = 0;` `    ``while` `(x != 0) {` `        ``if` `(x & 1)` `            ``parity++;` `        ``x = x >> 1;` `    ``}`   `    ``if` `(parity % 2 == 0)` `        ``return` `true``;` `    ``else` `        ``return` `false``;` `}`   `// Function to delete a node` `// in a Doubly Linked List.` `// head_ref --> pointer to head node pointer.` `// del --> pointer to node to be deleted` `void` `deleteNode(Node** head_ref, Node* del)` `{` `    ``// Base case` `    ``if` `(*head_ref == NULL || del == NULL)` `        ``return``;`   `    ``// If the node to be` `    ``// deleted is head node` `    ``if` `(*head_ref == del)` `        ``*head_ref = del->next;`   `    ``// Change next only if node to be` `    ``// deleted is not the last node` `    ``if` `(del->next != NULL)` `        ``del->next->prev = del->prev;`   `    ``// Change prev only if node to be` `    ``// deleted is not the first node` `    ``if` `(del->prev != NULL)` `        ``del->prev->next = del->next;`   `    ``// Finally, free the memory` `    ``// occupied by del` `    ``free``(del);`   `    ``return``;` `}`   `// Function to to remove all` `// the Even Parity Nodes from a` `// doubly linked list` `void` `deleteEvenParityNodes(` `    ``Node** head_ref)` `{` `    ``Node* ptr = *head_ref;` `    ``Node* next;`   `    ``// Iterating through` `    ``// the linked list` `    ``while` `(ptr != NULL) {` `        ``next = ptr->next;`   `        ``// If node's data's parity` `        ``// is even` `        ``if` `(isEvenParity(ptr->data))` `            ``deleteNode(head_ref, ptr);`   `        ``ptr = next;` `    ``}` `}`   `// Function to print nodes in a` `// given doubly linked list` `void` `printList(Node* head)` `{` `    ``if` `(head == NULL) {` `        ``cout << ``"Empty list\n"``;` `        ``return``;` `    ``}`   `    ``while` `(head != NULL) {` `        ``cout << head->data << ``" "``;` `        ``head = head->next;` `    ``}` `}`   `// Driver Code` `int` `main()` `{`   `    ``Node* head = NULL;`   `    ``// Create the doubly linked list` `    ``// 18 <-> 15 <-> 8 <-> 9 <-> 14` `    ``push(&head, 14);` `    ``push(&head, 9);` `    ``push(&head, 8);` `    ``push(&head, 15);` `    ``push(&head, 18);`   `    ``// Uncomment to view the list` `    ``// cout << "Original List: ";` `    ``// printList(head);`   `    ``deleteEvenParityNodes(&head);`   `    ``// Modified List` `    ``printList(head);` `}`

Output:
```8 14

```

Below is the implementation of the above approach:

 `// C++ program to remove all` `// the Even Parity Nodes from a` `// circular singly linked list`   `#include ` `using` `namespace` `std;`   `// Structure for a node` `struct` `Node {` `    ``int` `data;` `    ``struct` `Node* next;` `};`   `// Function to insert a node at the beginning` `// of a Circular linked list` `void` `push(``struct` `Node** head_ref, ``int` `data)` `{` `    ``// Create a new node` `    ``// and make head as next` `    ``// of it.` `    ``struct` `Node* ptr1` `        ``= (``struct` `Node*)``malloc``(` `            ``sizeof``(``struct` `Node));`   `    ``struct` `Node* temp = *head_ref;` `    ``ptr1->data = data;` `    ``ptr1->next = *head_ref;`   `    ``// If linked list is not NULL then` `    ``// set the next of last node` `    ``if` `(*head_ref != NULL) {`   `        ``// Find the node before head` `        ``// and update next of it.` `        ``while` `(temp->next != *head_ref)` `            ``temp = temp->next;`   `        ``temp->next = ptr1;` `    ``}` `    ``else`   `        ``// Point for the first node` `        ``ptr1->next = ptr1;`   `    ``*head_ref = ptr1;` `}`   `// Function to delete the node from a` `// Circular Linked list` `void` `deleteNode(` `    ``Node*& head_ref, Node* del)` `{` `    ``// If node to be deleted is head node` `    ``if` `(head_ref == del)` `        ``head_ref = del->next;`   `    ``struct` `Node* temp = head_ref;`   `    ``// Traverse list till not found` `    ``// delete node` `    ``while` `(temp->next != del) {` `        ``temp = temp->next;` `    ``}`   `    ``// Copy the address of the node` `    ``temp->next = del->next;`   `    ``// Finally, free the memory` `    ``// occupied by del` `    ``free``(del);`   `    ``return``;` `}`   `// Function that returns true if count` `// of set bits in x is even` `bool` `isEvenParity(``int` `x)` `{` `    ``// parity will store the` `    ``// count of set bits` `    ``int` `parity = 0;` `    ``while` `(x != 0) {` `        ``if` `(x & 1)` `            ``parity++;` `        ``x = x >> 1;` `    ``}`   `    ``if` `(parity % 2 == 0)` `        ``return` `true``;` `    ``else` `        ``return` `false``;` `}`   `// Function to delete all` `// the Even Parity Nodes` `// from the singly circular linked list` `void` `deleteEvenParityNodes(Node*& head)` `{` `    ``if` `(head == NULL)` `        ``return``;`   `    ``if` `(head == head->next) {` `        ``if` `(isEvenParity(head->data))` `            ``head = NULL;` `        ``return``;` `    ``}`   `    ``struct` `Node* ptr = head;`   `    ``struct` `Node* next;`   `    ``// Traverse the list till the end` `    ``do` `{` `        ``next = ptr->next;`   `        ``// If the node's data has even parity,` `        ``// delete node 'ptr'` `        ``if` `(isEvenParity(ptr->data))` `            ``deleteNode(head, ptr);`   `        ``// Point to the next node` `        ``ptr = next;`   `    ``} ``while` `(ptr != head);`   `    ``if` `(head == head->next) {` `        ``if` `(isEvenParity(head->data))` `            ``head = NULL;` `        ``return``;` `    ``}` `}`   `// Function to print nodes in a` `// given Circular linked list` `void` `printList(``struct` `Node* head)` `{` `    ``if` `(head == NULL) {` `        ``cout << ``"Empty List\n"``;` `        ``return``;` `    ``}`   `    ``struct` `Node* temp = head;` `    ``if` `(head != NULL) {` `        ``do` `{` `            ``printf``(``"%d "``, temp->data);` `            ``temp = temp->next;` `        ``} ``while` `(temp != head);` `    ``}` `}`   `// Driver code` `int` `main()` `{` `    ``// Initialize lists as empty` `    ``struct` `Node* head = NULL;`   `    ``// Created linked list will be` `    ``// 11->9->34->6->13->21` `    ``push(&head, 21);` `    ``push(&head, 13);` `    ``push(&head, 6);` `    ``push(&head, 34);` `    ``push(&head, 9);` `    ``push(&head, 11);`   `    ``deleteEvenParityNodes(head);`   `    ``printList(head);`   `    ``return` `0;` `}`

 `// Java program to remove all` `// the Even Parity Nodes from a` `// circular singly linked list` `class` `GFG{`   `// Structure for a node` `static` `class` `Node ` `{` `    ``int` `data;` `    ``Node next;` `};`   `// Function to insert a node at` `// the beginning of a Circular` `// linked list` `static` `Node push(Node head_ref, ``int` `data)` `{` `    `  `    ``// Create a new node` `    ``// and make head as next` `    ``// of it.` `    ``Node ptr1 = ``new` `Node();`   `    ``Node temp = head_ref;` `    ``ptr1.data = data;` `    ``ptr1.next = head_ref;`   `    ``// If linked list is not null then` `    ``// set the next of last node` `    ``if` `(head_ref != ``null``)` `    ``{` `        `  `        ``// Find the node before head` `        ``// and update next of it.` `        ``while` `(temp.next != head_ref)` `            ``temp = temp.next;`   `        ``temp.next = ptr1;` `    ``}` `    ``else`   `        ``// Point for the first node` `        ``ptr1.next = ptr1;`   `    ``head_ref = ptr1;` `    ``return` `head_ref;` `}`   `// Function to delete the node ` `// from a Circular Linked list` `static` `void` `deleteNode(Node head_ref,` `                       ``Node del)` `{` `    `  `    ``// If node to be deleted is` `    ``// head node` `    ``if` `(head_ref == del)` `        ``head_ref = del.next;`   `    ``Node temp = head_ref;`   `    ``// Traverse list till not found` `    ``// delete node` `    ``while` `(temp.next != del)` `    ``{` `        ``temp = temp.next;` `    ``}`   `    ``// Copy the address of the node` `    ``temp.next = del.next;`   `    ``// Finally, free the memory` `    ``// occupied by del` `    ``System.gc();`   `    ``return``;` `}`   `// Function that returns true if count` `// of set bits in x is even` `static` `boolean` `isEvenParity(``int` `x)` `{` `    `  `    ``// Parity will store the` `    ``// count of set bits` `    ``int` `parity = ``0``;` `    `  `    ``while` `(x != ``0``)` `    ``{` `        ``if` `((x & ``1``) != ``0``)` `            ``parity++;` `            `  `        ``x = x >> ``1``;` `    ``}`   `    ``if` `(parity % ``2` `== ``0``)` `        ``return` `true``;` `    ``else` `        ``return` `false``;` `}`   `// Function to delete all the ` `// Even Parity Nodes from the` `// singly circular linked list` `static` `void` `deleteEvenParityNodes(Node head)` `{` `    ``if` `(head == ``null``)` `        ``return``;`   `    ``if` `(head == head.next) ` `    ``{` `        ``if` `(isEvenParity(head.data))` `            ``head = ``null``;` `            `  `        ``return``;` `    ``}`   `    ``Node ptr = head;` `    ``Node next;`   `    ``// Traverse the list till the end` `    ``do` `    ``{` `        ``next = ptr.next;`   `        ``// If the node's data has ` `        ``// even parity, delete node 'ptr'` `        ``if` `(isEvenParity(ptr.data))` `            ``deleteNode(head, ptr);`   `        ``// Point to the next node` `        ``ptr = next;`   `    ``} ``while` `(ptr != head);`   `    ``if` `(head == head.next)` `    ``{` `        ``if` `(isEvenParity(head.data))` `            ``head = ``null``;` `            `  `        ``return``;` `    ``}` `}`   `// Function to print nodes in a` `// given Circular linked list` `static` `void` `printList(Node head)` `{` `    ``if` `(head == ``null``)` `    ``{` `        ``System.out.print(``"Empty List\n"``);` `        ``return``;` `    ``}`   `    ``Node temp = head;` `    ``if` `(head != ``null``)` `    ``{` `        ``do` `        ``{` `            ``System.out.printf(``"%d "``, temp.data);` `            ``temp = temp.next;` `        ``} ``while` `(temp != head);` `    ``}` `}`   `// Driver code` `public` `static` `void` `main(String[] args)` `{` `    `  `    ``// Initialize lists as empty` `    ``Node head = ``null``;`   `    ``// Created linked list will be` `    ``// 11.9.34.6.13.21` `    ``head = push(head, ``21``);` `    ``head = push(head, ``13``);` `    ``head = push(head, ``6``);` `    ``head = push(head, ``34``);` `    ``head = push(head, ``9``);` `    ``head = push(head, ``11``);`   `    ``deleteEvenParityNodes(head);`   `    ``printList(head);` `}` `}`   `// This code is contributed by Amit Katiyar`

 `// C# program to remove all` `// the Even Parity Nodes from a` `// circular singly linked list` `using` `System;` `class` `GFG{`   `// Structure for a node` `public` `class` `Node ` `{` `  ``public` `int` `data;` `  ``public` `Node next;` `};`   `// Function to insert a node at` `// the beginning of a Circular` `// linked list` `static` `Node push(Node head_ref, ` `                 ``int` `data)` `{    ` `  ``// Create a new node` `  ``// and make head as next` `  ``// of it.` `  ``Node ptr1 = ``new` `Node();`   `  ``Node temp = head_ref;` `  ``ptr1.data = data;` `  ``ptr1.next = head_ref;`   `  ``// If linked list is not ` `  ``// null then set the next ` `  ``// of last node` `  ``if` `(head_ref != ``null``)` `  ``{` `    ``// Find the node before head` `    ``// and update next of it.` `    ``while` `(temp.next != head_ref)` `      ``temp = temp.next;`   `    ``temp.next = ptr1;` `  ``}` `  ``else`   `    ``// Point for the first node` `    ``ptr1.next = ptr1;`   `  ``head_ref = ptr1;` `  ``return` `head_ref;` `}`   `// Function to delete the node ` `// from a Circular Linked list` `static` `void` `deleteNode(Node head_ref,` `                       ``Node del)` `{    ` `  ``// If node to be deleted is` `  ``// head node` `  ``if` `(head_ref == del)` `    ``head_ref = del.next;`   `  ``Node temp = head_ref;`   `  ``// Traverse list till not ` `  ``// found delete node` `  ``while` `(temp.next != del)` `  ``{` `    ``temp = temp.next;` `  ``}`   `  ``// Copy the address of ` `  ``// the node` `  ``temp.next = del.next;`   `  ``return``;` `}`   `// Function that returns true ` `// if count of set bits in x ` `// is even` `static` `bool` `isEvenParity(``int` `x)` `{` `  ``// Parity will store the` `  ``// count of set bits` `  ``int` `parity = 0;`   `  ``while` `(x != 0)` `  ``{` `    ``if` `((x & 1) != 0)` `      ``parity++;`   `    ``x = x >> 1;` `  ``}`   `  ``if` `(parity % 2 == 0)` `    ``return` `true``;` `  ``else` `    ``return` `false``;` `}`   `// Function to delete all the ` `// Even Parity Nodes from the` `// singly circular linked list` `static` `void` `deleteEvenParityNodes(Node head)` `{` `  ``if` `(head == ``null``)` `    ``return``;`   `  ``if` `(head == head.next) ` `  ``{` `    ``if` `(isEvenParity(head.data))` `      ``head = ``null``;`   `    ``return``;` `  ``}`   `  ``Node ptr = head;` `  ``Node next;`   `  ``// Traverse the list ` `  ``// till the end` `  ``do` `  ``{` `    ``next = ptr.next;`   `    ``// If the node's data has ` `    ``// even parity, delete node 'ptr'` `    ``if` `(isEvenParity(ptr.data))` `      ``deleteNode(head, ptr);`   `    ``// Point to the next node` `    ``ptr = next;`   `  ``} ``while` `(ptr != head);`   `  ``if` `(head == head.next)` `  ``{` `    ``if` `(isEvenParity(head.data))` `      ``head = ``null``;`   `    ``return``;` `  ``}` `}`   `// Function to print nodes in a` `// given Circular linked list` `static` `void` `printList(Node head)` `{` `  ``if` `(head == ``null``)` `  ``{` `    ``Console.Write(``"Empty List\n"``);` `    ``return``;` `  ``}`   `  ``Node temp = head;` `  ``if` `(head != ``null``)` `  ``{` `    ``do` `    ``{` `      ``Console.Write(temp.data + ``" "``);` `      ``temp = temp.next;` `    ``} ``while` `(temp != head);` `  ``}` `}`   `// Driver code` `public` `static` `void` `Main(String[] args)` `{    ` `  ``// Initialize lists as empty` `  ``Node head = ``null``;`   `  ``// Created linked list will be` `  ``// 11.9.34.6.13.21` `  ``head = push(head, 21);` `  ``head = push(head, 13);` `  ``head = push(head, 6);` `  ``head = push(head, 34);` `  ``head = push(head, 9);` `  ``head = push(head, 11);`   `  ``deleteEvenParityNodes(head);` `  ``printList(head);` `}` `}`   `// This code is contributed by Rajput-Ji`

Output:
```11 13 21

```

Time Complexity: O(K*N), where N is the size of the linked list and K is the number of bits in the maximum number present in the linked list.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : amit143katiyar, Rajput-Ji