First common element in two linked lists
Last Updated :
03 Aug, 2022
Given two Linked Lists, find the first common element between given linked list i.e., we need to find first node of the first list which is also present in the 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 the first list and for every node, we search it in the second list. As soon as we find an element in the second list, we return it.
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
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;
}
int firstCommon( struct Node* head1, struct Node* head2)
{
for (; head1 != NULL; head1=head1->next)
for (Node *p = head2; p != NULL; p = p->next)
if (p->data == head1->data)
return head1->data;
return 0;
}
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;
}
|
Java
import java.util.*;
class GFG
{
static class Node
{
int data;
Node next;
};
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
static int firstCommon(Node head1, Node head2)
{
for (; head1 != null ; head1 = head1.next)
for (Node p = head2; p != null ; p = p.next)
if (p.data == head1.data)
return head1.data;
return 0 ;
}
public static void main(String[] args)
{
Node head1 = null ;
head1 = push(head1, 20 );
head1 = push(head1, 5 );
head1 = push(head1, 15 );
head1 = push(head1, 10 );
Node head2 = null ;
head2 = push(head2, 10 );
head2 = push(head2, 2 );
head2 = push(head2, 15 );
head2 = push(head2, 8 );
System.out.println(firstCommon(head1, head2));
}
}
|
Python3
import math
class Node:
def __init__( self , data):
self .data = data
self . next = None
def push(head_ref, new_data):
new_node = Node(new_data)
new_node.data = new_data
new_node. next = head_ref
head_ref = new_node
return head_ref
def firstCommon(head1, head2):
while (head1 ! = None ):
p = head2
while (p ! = None ):
if (p.data = = head1.data):
return head1.data
p = p. next
head1 = head1. next
return 0
if __name__ = = '__main__' :
head1 = None
head1 = push(head1, 20 )
head1 = push(head1, 5 )
head1 = push(head1, 15 )
head1 = push(head1, 10 )
head2 = None
head2 = push(head2, 10 )
head2 = push(head2, 2 )
head2 = push(head2, 15 )
head2 = push(head2, 8 )
print (firstCommon(head1, head2))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
class Node
{
public int data;
public Node next;
};
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
static int firstCommon(Node head1, Node head2)
{
for (; head1 != null ; head1 = head1.next)
for (Node p = head2; p != null ; p = p.next)
if (p.data == head1.data)
return head1.data;
return 0;
}
public static void Main(String[] args)
{
Node head1 = null ;
head1 = push(head1, 20);
head1 = push(head1, 5);
head1 = push(head1, 15);
head1 = push(head1, 10);
Node head2 = null ;
head2 = push(head2, 10);
head2 = push(head2, 2);
head2 = push(head2, 15);
head2 = push(head2, 8);
Console.WriteLine(firstCommon(head1, head2));
}
}
|
Javascript
<script>
class Node
{
constructor()
{
this .data = 0;
this .next = null ;
}
};
function push(head_ref, new_data)
{
var new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
function firstCommon(head1, head2)
{
for (; head1 != null ; head1 = head1.next)
for ( var p = head2; p != null ; p = p.next)
if (p.data == head1.data)
return head1.data;
return 0;
}
var head1 = null ;
head1 = push(head1, 20);
head1 = push(head1, 5);
head1 = push(head1, 15);
head1 = push(head1, 10);
var head2 = null ;
head2 = push(head2, 10);
head2 = push(head2, 2);
head2 = push(head2, 15);
head2 = push(head2, 8);
document.write(firstCommon(head1, head2));
</script>
|
Output:
10
Time complexity: O(M*N) where M and N are size of given linked lists
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...