# First common element in two linked lists

Given two Linked Lists, find the first common element between given linked list i.e., we need to find first node of first list which is also present in second list.

Examples:

Input : List1: 10->15->4->20 Lsit2: 8->4->2->10 Output : 10 Input : List1: 1->2->3->4 Lsit2: 5->6->3->8 Output : 3

We traverse first list and for every node, we search it in second list. As soon as we find an element in second list, we return it.

`// C++ program to find first common element in ` `// two unsorted linked list ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `/* Link list node */` `struct` `Node { ` ` ` `int` `data; ` ` ` `struct` `Node* next; ` `}; ` ` ` `/* A utility function to insert a node at the ` ` ` `beginning of a linked list*/` `void` `push(` `struct` `Node** head_ref, ` `int` `new_data) ` `{ ` ` ` `struct` `Node* new_node = ` ` ` `(` `struct` `Node*)` `malloc` `(` `sizeof` `(` `struct` `Node)); ` ` ` `new_node->data = new_data; ` ` ` `new_node->next = (*head_ref); ` ` ` `(*head_ref) = new_node; ` `} ` ` ` `/* Returns the first repeating element in linked list*/` `int` `firstCommon(` `struct` `Node* head1, ` `struct` `Node* head2) ` `{ ` ` ` `// Traverse through every node of first list ` ` ` `for` `(; head1 != NULL; head1=head1->next) ` ` ` ` ` `// If current node is present in second list ` ` ` `for` `(Node *p = head2; p != NULL; p = p->next) ` ` ` `if` `(p->data == head1->data) ` ` ` `return` `head1->data; ` ` ` ` ` `// If no common node ` ` ` `return` `0; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `struct` `Node* head1 = NULL; ` ` ` `push(&head1, 20); ` ` ` `push(&head1, 5); ` ` ` `push(&head1, 15); ` ` ` `push(&head1, 10); ` ` ` ` ` `struct` `Node* head2 = NULL; ` ` ` `push(&head2, 10); ` ` ` `push(&head2, 2); ` ` ` `push(&head2, 15); ` ` ` `push(&head2, 8); ` ` ` ` ` `cout << firstCommon(head1, head2); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

10

## Recommended Posts:

- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Longest common suffix of two linked lists
- Find common elements in three linked lists
- Create a linked list from two linked lists by choosing max element at each position
- Find count of common nodes in two Doubly Linked Lists
- Count items common to both the lists but with different prices
- Identical Linked Lists
- Longest Common Prefix using Linked List
- Add Two Numbers Represented by Linked Lists | Set 3
- Add two numbers represented by linked lists | Set 1
- Union and Intersection of two Linked Lists
- Merge two sorted linked lists
- Merge K sorted linked lists | Set 1
- Merge Sort for Linked Lists
- Intersection of two Sorted Linked Lists

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.