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 <=> 6
Output : Number of common nodes: 4
Input :
list 1 = 18 <=> 30 <=> 92 <=> 46 <=> 72 <=> 1
list 2 = 12 <=> 32 <=> 45 <=> 9 <=> 6 <=> 30
Output : Number of common nodes: 1
Approach 1: 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.
Algorithm:
- Create a function called “push” that adds a new node to the doubly linked list’s beginning. The function requires two arguments: new data and head ref (a pointer to the list’s head) (the data value to be inserted). The updated head pointer is returned by the function.
- Create a function called “countCommonNodes” that accepts the parameters head ref (a pointer to the first doubly linked list’s head) and head (pointer to the head of the second doubly linked list). The number of shared nodes between the two lists is represented as an integer by the function’s output. s.
- Set up two pointers, ptr, and ptr1, to point to the respective heads of the two lists.
- Set the count variable’s initial value to 0.
- Use ptr to navigate through the first list to the very end.
- Up to the list’s conclusion, traverse the second list using ptr1.
- Increase the count and exit the inner loop if the data value of the current node in the first list equals the data value of the current node in the second list.
- Reset ptr1 to the head of the second list.
- Move ptr to the next node in the first list.
- Repeat steps 6-10 until the end of the first list is reached.
- Return the count of common nodes.
Below is the implementation of the above approach:
C++
// 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 beginning, // prev is always NULL new_node->prev = NULL; // link the old list of 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; // initialize count = 0 int count = 0; // traverse list 1 till the end while (ptr != NULL) { // traverse 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; } |
Java
// Java implementation to count // common element in given two // doubly linked list class GFG { // Node of the doubly linked list static class Node { int data; Node prev, next; }; // Function to insert a node at the beginning // of the Doubly Linked List static Node push(Node head_ref, int new_data) { // allocate node Node new_node = new Node(); // put in 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 of 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; return head_ref; } // Count common nodes in both list1 and list 2 static int countCommonNodes(Node head_ref, Node head) { // head for list 1 Node ptr = head_ref; // head for list 2 Node ptr1 = head; // initialize count = 0 int count = 0 ; // traverse list 1 till the end while (ptr != null ) { // traverse 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 Code public static void main(String[] args) { // start with the empty list Node head = null ; Node head1 = null ; // create the doubly linked list 1 // 15 <. 16 <. 10 <. 9 <. 6 <. 7 <. 17 head = push(head, 17 ); head = push(head, 7 ); head = push(head, 6 ); head = push(head, 9 ); head = push(head, 10 ); head = push(head, 16 ); head = push(head, 15 ); // create the doubly linked list 2 // 15 <. 16 <. 45 <. 9 <. 6 head1 = push(head1, 6 ); head1 = push(head1, 9 ); head1 = push(head1, 45 ); head1 = push(head1, 16 ); head1 = push(head1, 15 ); System.out.println( "Number of common nodes: " + countCommonNodes(head, head1)); } } // This code is contributed by Rajput-Ji |
Python3
# Python3 implementation to count # common element in given two # doubly linked list # Node of the doubly linked list class Node: def __init__( self , data): self .data = data self .prev = None self . next = None # Function to insert a node at the beginning # of the Doubly Linked List def push(head_ref, new_data): # allocate node new_node = Node(new_data) # put in the data new_node.data = new_data; # since we are adding at the beginning, # prev is always None new_node.prev = None ; # link the old list of the new node new_node. next = (head_ref); # change prev of head node to new node if ((head_ref) ! = None ): (head_ref).prev = new_node; # move the head to point to the new node (head_ref) = new_node; return head_ref # Count common nodes in both list1 and list 2 def countCommonNodes(head_ref, head): # head for list 1 ptr = head_ref; # head for list 2 ptr1 = head; # initialize count = 0 count = 0 ; # traverse list 1 till the end while (ptr ! = None ): # traverse list 2 till the end while (ptr1 ! = None ): # if node value is equal then # increment count if (ptr.data = = ptr1.data): count + = 1 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 if __name__ = = '__main__' : # start with the empty list head = None ; head1 = None ; # create the doubly linked list 1 # 15 <. 16 <. 10 <. 9 <. 6 <. 7 <. 17 head = push(head, 17 ); head = push(head, 7 ); head = push(head, 6 ); head = push(head, 9 ); head = push(head, 10 ); head = push(head, 16 ); head = push( head, 15 ); # create the doubly linked list 2 # 15 <. 16 <. 45 <. 9 <. 6 head1 = push(head1, 6 ); head1 = push(head1, 9 ); head1 = push(head1, 45 ); head1 = push(head1, 16 ); head1 = push(head1, 15 ); print ( "Number of common nodes: " + str (countCommonNodes(head, head1))) # This code is contributed by rutvik_56. |
C#
// C# implementation to count // common element in given two // doubly linked list using System; class GFG { // Node of the doubly linked list public class Node { public int data; public Node prev, next; }; // Function to insert a node at the beginning // of the Doubly Linked List static Node push(Node head_ref, int new_data) { // allocate node Node new_node = new Node(); // put in 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 of 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; return head_ref; } // Count common nodes in both list1 and list 2 static int countCommonNodes(Node head_ref, Node head) { // head for list 1 Node ptr = head_ref; // head for list 2 Node ptr1 = head; // initialize count = 0 int count = 0; // traverse list 1 till the end while (ptr != null ) { // traverse 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 Code public static void Main(String[] args) { // start with the empty list Node head = null ; Node head1 = null ; // create the doubly linked list 1 // 15 <. 16 <. 10 <. 9 <. 6 <. 7 <. 17 head = push(head, 17); head = push(head, 7); head = push(head, 6); head = push(head, 9); head = push(head, 10); head = push(head, 16); head = push(head, 15); // create the doubly linked list 2 // 15 <. 16 <. 45 <. 9 <. 6 head1 = push(head1, 6); head1 = push(head1, 9); head1 = push(head1, 45); head1 = push(head1, 16); head1 = push(head1, 15); Console.WriteLine( "Number of common nodes: " + countCommonNodes(head, head1)); } } // This code is contributed by PrinciRaj1992 |
Javascript
<script> // JavaScript implementation to count // common element in given two // doubly linked list // Node of the doubly linked list class Node { constructor(val) { this .data = val; this .prev = null ; this .next = null ; } } // Function to insert a node at the beginning // of the Doubly Linked List function push(head_ref , new_data) { // allocate node var new_node = new Node(); // put in 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 of 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; return head_ref; } // Count common nodes in both list1 and list 2 function countCommonNodes(head_ref, head) { // head for list 1 var ptr = head_ref; // head for list 2 var ptr1 = head; // initialize count = 0 var count = 0; // traverse list 1 till the end while (ptr != null ) { // traverse 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 Code // start with the empty list var head = null ; var head1 = null ; // create the doubly linked list 1 // 15 <. 16 <. 10 <. 9 <. 6 <. 7 <. 17 head = push(head, 17); head = push(head, 7); head = push(head, 6); head = push(head, 9); head = push(head, 10); head = push(head, 16); head = push(head, 15); // create the doubly linked list 2 // 15 <. 16 <. 45 <. 9 <. 6 head1 = push(head1, 6); head1 = push(head1, 9); head1 = push(head1, 45); head1 = push(head1, 16); head1 = push(head1, 15); document.write( "Number of common nodes: " + countCommonNodes(head, head1)); // This code contributed by umadevi9616 </script> |
Number of common nodes:4
Complexity Analysis:
- Time Complexity: O(n1*n2) where n1 is the length of the first linked list and n2 is the length of the second linked list
- Auxiliary Space: O(1)
Approach 2: Use hash tables or sets to keep track of the values in one list and then traverse the other list to count the common nodes.
Algorithm:
- Define a struct Node which consists of an integer data value, a pointer to the previous node, and a pointer to the next node.
- Create a push function to insert a new node at the beginning of the doubly linked list. This function takes the address of the head pointer and the new data as inputs.
- Create a countCommonNodes function to count the number of common nodes in two doubly linked lists. This function takes the addresses of the head pointers of both lists as inputs and returns an integer value.
- Inside the countCommonNodes function, initialize two node pointers ptr and ptr1 to the head of the first and second linked lists, respectively.
- Initialize a count variable to 0 to keep track of the number of common nodes.
- Traverse the first linked list using ptr and for each node of the first list, traverse the second linked list using ptr1.
- If the data value of the current node in the first list matches the data value of any node in the second list, increment the count variable and break the inner loop.
- After the inner loop completes, reset ptr1 to the head of the second list.
Move ptr to the next node of the first list and repeat steps 6-8 until ptr reaches the end of the first list. - Return the count of common nodes.
- In the main function, create two doubly linked lists using push function and call the countCommonNodes function passing the head pointers of both the lists as inputs.
- Print the returned count value.
Below is the implementation of the above approach:
C++
//C++ code for above approach #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 beginning, // prev is always NULL new_node->prev = NULL; // link the old list of 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; // initialize count = 0 int count = 0; // set to store values of list 1 unordered_set< int > values; // insert values of list 1 in the set while (ptr != NULL) { values.insert(ptr->data); ptr = ptr->next; } // traverse list 2 and count common nodes while (ptr1 != NULL) { if (values.count(ptr1->data) > 0) { count++; } ptr1 = ptr1->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; } |
Java
import java.util.HashSet; // Node of the doubly linked list class Node { int data; Node prev, next; } public class CommonNodeCount { // Function to insert a node at the beginning // of the Doubly Linked List static void push(Node[] headRef, int newData) { // Allocate node Node newNode = new Node(); // Put in the data newNode.data = newData; // Since we are adding at the beginning, // prev is always null newNode.prev = null ; // Link the old list to the new node newNode.next = headRef[ 0 ]; // Change prev of the head node to new node if (headRef[ 0 ] != null ) { headRef[ 0 ].prev = newNode; } // Move the head to point to the new node headRef[ 0 ] = newNode; } // Count common nodes in both list1 and list2 static int countCommonNodes(Node[] headRef, Node[] head) { // Head for list1 Node ptr = headRef[ 0 ]; // Head for list2 Node ptr1 = head[ 0 ]; // Initialize count = 0 int count = 0 ; // Set to store values of list1 HashSet<Integer> values = new HashSet<>(); // Insert values of list1 in the set while (ptr != null ) { values.add(ptr.data); ptr = ptr.next; } // Traverse list2 and count common nodes while (ptr1 != null ) { if (values.contains(ptr1.data)) { count++; } ptr1 = ptr1.next; } // Return count of common nodes return count; } // Driver program public static void main(String[] args) { // 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 ); System.out.println( "Number of common nodes: " + countCommonNodes(head, head1)); } } // This code is contributed by Samim Hossain Mondal. |
Python3
#Python3 approach for the above code # Node of the doubly linked list class Node: def __init__( self , data): self .data = data self .prev = None self . next = None # Function to insert a node at the beginning # of the Doubly Linked List def push(head_ref, new_data): # allocate node new_node = Node(new_data) # link the old list of the new node new_node. next = head_ref # change prev of head node to new node if head_ref ! = None : head_ref.prev = new_node # move the head to point to the new node head_ref = new_node return head_ref # Count common nodes in both list1 and list 2 def countCommonNodes(head_ref, head): # head for list 1 ptr = head_ref # head for list 2 ptr1 = head # initialize count = 0 count = 0 # set to store values of list 1 values = set () # insert values of list 1 in the set while ptr ! = None : values.add(ptr.data) ptr = ptr. next # traverse list 2 and count common nodes while ptr1 ! = None : if ptr1.data in values: count + = 1 ptr1 = ptr1. next # return count of common nodes return count # Driver program if __name__ = = '__main__' : # start with the empty list head = None head1 = None # create the doubly linked list 1 # 15 <-> 16 <-> 10 <-> 9 <-> 6 <-> 7 <-> 17 head = push(head, 17 ) head = push(head, 7 ) head = push(head, 6 ) head = push(head, 9 ) head = push(head, 10 ) head = push(head, 16 ) head = push(head, 15 ) # create the doubly linked list 2 # 15 <-> 16 <-> 45 <-> 9 <-> 6 head1 = push(head1, 6 ) head1 = push(head1, 9 ) head1 = push(head1, 45 ) head1 = push(head1, 16 ) head1 = push(head1, 15 ) print ( "Number of common nodes:" , countCommonNodes(head, head1)) |
C#
using System; using System.Collections.Generic; // Node of the doubly linked list public class Node { public int data; public Node prev, next; } class Program { // Function to insert a node at the beginning // of the Doubly Linked List static void Push( ref Node head_ref, int new_data) { // allocate node Node new_node = new Node(); // put in 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 to 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 list2 static int CountCommonNodes( ref Node head_ref, ref Node head) { // head for list 1 Node ptr = head_ref; // head for list 2 Node ptr1 = head; // initialize count = 0 int count = 0; // set to store values of list 1 HashSet< int > values = new HashSet< int >(); // insert values of list 1 in the set while (ptr != null ) { values.Add(ptr.data); ptr = ptr.next; } // traverse list 2 and count common nodes while (ptr1 != null ) { if (values.Contains(ptr1.data)) { count++; } ptr1 = ptr1.next; } // return count of common nodes return count; } // Driver program static void 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( ref head, 17); Push( ref head, 7); Push( ref head, 6); Push( ref head, 9); Push( ref head, 10); Push( ref head, 16); Push( ref head, 15); // create the doubly linked list 2 // 15 <-> 16 <-> 45 <-> 9 <-> 6 Push( ref head1, 6); Push( ref head1, 9); Push( ref head1, 45); Push( ref head1, 16); Push( ref head1, 15); Console.WriteLine( "Number of common nodes: " + CountCommonNodes( ref head, ref head1)); } } |
Javascript
class Node { constructor(data) { this .data = data; this .prev = null ; this .next = null ; } } // Function to insert a node at the beginning of // the Doubly Linked List function push(head_ref, new_data) { // Create a new node const new_node = new Node(new_data); new_node.next = head_ref; if (head_ref !== null ) { head_ref.prev = new_node; } return new_node; } // Function to count common nodes in // both list1 and list2 function GFG(head_ref, head) { // Initialize count let count = 0; const values = new Set(); // Insert values of list1 in the set let ptr = head_ref; while (ptr !== null ) { values.add(ptr.data); ptr = ptr.next; } // Traverse list2 and count common nodes let ptr1 = head; while (ptr1 !== null ) { if (values.has(ptr1.data)) { count++; } ptr1 = ptr1.next; } // Return count of common nodes return count; } // Driver program function main() { let head = null ; let head1 = null ; // Create the doubly linked list 1 head = push(head, 17); head = push(head, 7); head = push(head, 6); head = push(head, 9); head = push(head, 10); head = push(head, 16); head = push(head, 15); head1 = push(head1, 6); head1 = push(head1, 9); head1 = push(head1, 45); head1 = push(head1, 16); head1 = push(head1, 15); // Count common nodes and // print the result const commonNodeCount = GFG(head, head1); console.log( "Number of common nodes: " + commonNodeCount); } main(); |
Output:
Number of common nodes:4
Time complexity: O(m*n)
The time complexity of the code is O(mn), where m and n are the sizes of the two input linked lists. This is because we have to traverse both linked lists completely to find common nodes. The nested while loop inside the function iterates over each node of the second list for each node of the first list. This results in a time complexity of O(mn).
Auxiliary space: O(1)
The auxiliary space complexity of the code is O(1) because we are not using any extra space for storing information. We are just using pointers to traverse the linked lists and an integer variable to store the count of common nodes. Therefore, the space complexity remains constant, i.e., O(1).
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!