# Find count of common nodes in two Doubly Linked Lists

Given two doubly linked lists. The task is to find the total number of common nodes in both the doubly linked list.

**Examples:**

Input :list 1 = 15 <=> 16 <=> 10 <=> 9 <=> 6 <=> 7 <=> 17 list 2 = 15 <=> 16 <=> 45 <=> 9 <=> 6Output :Number of common nodes: 4Input :list 1 = 18 <=> 30 <=> 92 <=> 46 <=> 72 <=> 1 list 2 = 12 <=> 32 <=> 45 <=> 9 <=> 6 <=> 30Output :Number of common nodes: 1

**Approach:** Traverse both lists till the end of the list using two nested loops. For every node in list 1 check if it matches with any node in list 2. If yes then increment the count of common nodes. Finally, print the count.

Below is the implementation of the above approach:

`// C++ implementation to count ` `// common element in given two ` `// doubly linked list ` `#include <bits/stdc++.h> ` ` ` `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 node ` ` ` `Node* new_node = (Node*)` `malloc` `(` `sizeof` `(` `struct` `Node)); ` ` ` ` ` `// put in the data ` ` ` `new_node->data = new_data; ` ` ` ` ` `// since we are adding at the begining, ` ` ` `// prev is always NULL ` ` ` `new_node->prev = NULL; ` ` ` ` ` `// link the old list off the new node ` ` ` `new_node->next = (*head_ref); ` ` ` ` ` `// change 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; ` `} ` ` ` `// Count common nodes in both list1 and list 2 ` `int` `countCommonNodes(Node** head_ref, Node** head) ` `{ ` ` ` `// head for list 1 ` ` ` `Node* ptr = *head_ref; ` ` ` ` ` `// head for list 2 ` ` ` `Node* ptr1 = *head; ` ` ` ` ` `// intialaize count = 0 ` ` ` `int` `count = 0; ` ` ` ` ` `// travers list 1 till the end ` ` ` `while` `(ptr != NULL) { ` ` ` `// travers list 2 till the end ` ` ` `while` `(ptr1 != NULL) { ` ` ` `// if node value is equal then ` ` ` `// increment count ` ` ` `if` `(ptr->data == ptr1->data) { ` ` ` `count++; ` ` ` `break` `; ` ` ` `} ` ` ` ` ` `// increment pointer list 2 ` ` ` `ptr1 = ptr1->next; ` ` ` `} ` ` ` ` ` `// again list 2 start with starting point ` ` ` `ptr1 = *head; ` ` ` ` ` `// increment pointer list 1 ` ` ` `ptr = ptr->next; ` ` ` `} ` ` ` ` ` `// return count of common nodes ` ` ` `return` `count; ` `} ` ` ` `// Driver program ` `int` `main() ` `{ ` ` ` `// start with the empty list ` ` ` `Node* head = NULL; ` ` ` `Node* head1 = NULL; ` ` ` ` ` `// create the doubly linked list 1 ` ` ` `// 15 <-> 16 <-> 10 <-> 9 <-> 6 <-> 7 <-> 17 ` ` ` `push(&head, 17); ` ` ` `push(&head, 7); ` ` ` `push(&head, 6); ` ` ` `push(&head, 9); ` ` ` `push(&head, 10); ` ` ` `push(&head, 16); ` ` ` `push(&head, 15); ` ` ` ` ` `// create the doubly linked list 2 ` ` ` `// 15 <-> 16 <-> 45 <-> 9 <-> 6 ` ` ` `push(&head1, 6); ` ` ` `push(&head1, 9); ` ` ` `push(&head1, 45); ` ` ` `push(&head1, 16); ` ` ` `push(&head1, 15); ` ` ` ` ` `cout << ` `"Number of common nodes:"` ` ` `<< countCommonNodes(&head, &head1); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Number of common nodes:4

## Recommended Posts:

- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Find common elements in three linked lists
- Find the common nodes in two singly linked list
- Hashtables Chaining with Doubly Linked Lists
- Rotate Doubly linked list by N nodes
- Delete all the even nodes from a Doubly Linked List
- First common element in two linked lists
- Delete all Prime Nodes from a Doubly Linked List
- Delete all the nodes from the doubly linked list that are greater than a given value
- Delete all nodes from the doubly linked list which are divisible by K
- Delete all the nodes from a doubly linked list that are smaller than a given value
- Sum of all nodes in a doubly linked list divisible by a given number K
- Product of all prime nodes in a Doubly Linked List
- Longest common suffix of two linked lists
- Product of all nodes in a doubly linked list divisible by a given number K

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.