Find extra node in the second Linked list
Last Updated :
15 Nov, 2022
Given two Linked list L1 and L2. The second list L2 contains all the nodes of L1 along with 1 extra node. The task is to find that extra node.
Examples:
Input: L1 = 17 -> 7 -> 6 -> 16
L2 = 17 -> 7 -> 6 -> 16 -> 15
Output: 15
Explanation:
Element 15 is not present in the L1 list
Input: L1 = 10 -> 15 -> 5
L2 = 10 -> 100 -> 15 -> 5
Output: 100
Naive approach:
- Run nested loops and find the nodes in L2 which is not present in L1.
- The time complexity of this approach will be O(N2) where N is the length of the linked list.
Efficient approach:
- If all the nodes of the L1 and L2 are XORed together then every node of A[] will give 0 with its occurrence in L2 and the extra element say X when XORed with 0 will give (X XOR 0) = X which is the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* next;
};
void push(Node** head_ref,
int new_data)
{
Node* new_node =
(Node*) malloc ( sizeof
( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int print(Node* head_ref,
Node* head_ref1)
{
int ans = 0;
Node* ptr1 = head_ref;
Node* ptr2 = head_ref1;
while (ptr1 != NULL) {
ans ^= ptr1->data;
ptr1 = ptr1->next;
}
while (ptr2 != NULL) {
ans ^= ptr2->data;
ptr2 = ptr2->next;
}
return ans;
}
int main()
{
Node* head1 = NULL;
Node* head2 = NULL;
push(&head1, 17);
push(&head1, 7);
push(&head1, 6);
push(&head1, 16);
push(&head2, 17);
push(&head2, 7);
push(&head2, 6);
push(&head2, 16);
push(&head1, 15);
int k = print(head1, head2);
cout << k;
return 0;
}
|
Java
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 void extra(Node head_ref1,
Node head_ref2)
{
int ans = 0 ;
Node ptr1 = head_ref1;
Node ptr2 = head_ref2;
while (ptr1 != null ) {
ans ^= ptr1.data;
ptr1 = ptr1.next;
}
while (ptr2 != null ) {
ans ^= ptr2.data;
ptr2 = ptr2.next;
}
System.out.println(ans);
}
public static void main(String args[])
{
Node head1 = null ;
Node head2 = null ;
head1 = push(head1, 17 );
head1 = push(head1, 7 );
head1 = push(head1, 6 );
head1 = push(head1, 16 );
head1 = push(head1, 15 );
head2 = push(head2, 17 );
head2 = push(head2, 7 );
head2 = push(head2, 6 );
head2 = push(head2, 16 );
extra(head1, head2);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = next
def push( head_ref, new_data) :
new_node = Node( 0 )
new_node.data = new_data
new_node. next = (head_ref)
(head_ref) = new_node
return head_ref
def extra(head_ref1, head_ref2) :
ans = 0
ptr1 = head_ref1
ptr2 = head_ref2
while (ptr1 ! = None ):
ans ^ = ptr1.data
ptr1 = ptr1. next
while (ptr2 ! = None ):
ans^ = ptr2.data
ptr2 = ptr2. next
print (ans)
head1 = None
head2 = None
head1 = push(head1, 17 )
head1 = push(head1, 7 )
head1 = push(head1, 6 )
head1 = push(head1, 16 )
head1 = push(head1, 15 )
head2 = push(head2, 17 )
head2 = push(head2, 7 )
head2 = push(head2, 6 )
head2 = push(head2, 16 )
extra(head1, head2)
|
C#
using System;
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 void extra(Node head_ref1,
Node head_ref2)
{
int ans = 0;
Node ptr1 = head_ref1;
Node ptr2 = head_ref2;
while (ptr1 != null )
{
ans ^= ptr1.data;
ptr1 = ptr1.next;
}
while (ptr2 != null )
{
ans ^= ptr2.data;
ptr2 = ptr2.next;
}
Console.WriteLine(ans);
}
public static void Main(String []args)
{
Node head1 = null ;
Node head2 = null ;
head1 = push(head1, 17);
head1 = push(head1, 7);
head1 = push(head1, 6);
head1 = push(head1, 16);
head1 = push(head1, 15);
head2 = push(head2, 17);
head2 = push(head2, 7);
head2 = push(head2, 6);
head2 = push(head2, 16);
extra(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 print(head_ref, head_ref1)
{
var ans = 0;
var ptr1 = head_ref;
var ptr2 = head_ref1;
while (ptr1 != null ) {
ans ^= ptr1.data;
ptr1 = ptr1.next;
}
while (ptr2 != null ) {
ans ^= ptr2.data;
ptr2 = ptr2.next;
}
return ans;
}
var head1 = null ;
var head2 = null ;
head1 = push(head1, 17);
head1 = push(head1, 7);
head1 = push(head1, 6);
head1 = push(head1, 16);
head2 = push(head2, 17);
head2 = push(head2, 7);
head2 = push(head2, 6);
head2 = push(head2, 16);
head1 = push(head1, 15);
var k = print(head1, head2);
document.write( k);
</script>
|
Time Complexity: O(N)
Space Complexity: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...