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

• Difficulty Level : Medium
• Last Updated : 10 Jan, 2022

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:

Python3

Output:

8 14

Below is the implementation of the above approach:

Javascript



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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :