Given two doubly linked lists. The task is to find the total number of common nodes in both the doubly linked list.
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: 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:
Number of common nodes:4
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.
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Remove the common nodes in two Singly Linked Lists
- Sorted merge of two sorted doubly circular linked lists
- Longest common suffix of two linked lists
- First common element in two linked lists
- Hashtables Chaining with Doubly Linked Lists
- Find the common nodes in two singly linked list
- Find common elements in three linked lists
- Merge odd and even positioned nodes of two Linked Lists alternately
- Rotate Doubly linked list by N nodes
- Delete all the nodes from the doubly linked list that are greater than a given value
- Delete all the even nodes from a Doubly Linked List
- Product of all prime nodes in a Doubly Linked List
- 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
- Replace even nodes of a doubly linked list with the elements of array
- Delete all Prime Nodes from a Doubly Linked List
- Delete all nodes from the doubly linked list which are divisible by K
- Product of all nodes in a doubly linked list divisible by a given number K
- Remove all nodes from a Doubly Linked List containing Fibonacci numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.