Given a singly linked list of integers. The task is to check if each element in the linked list is present in a pair i.e. all elements occur even no. of times.
Examples:
Input: 1 -> 2 -> 3 -> 3 -> 1 -> 2
Output: Yes
Input: 10 -> 20 -> 30 -> 20
Output: No
Approach:
- Initialize a temp node pointing to head.
- Take a variable to calculate XOR of all elements.
- Start traversing linked list and keep calculating the XOR with node->data.
- Return true if XOR is 0, else return false.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
bool isPair( struct Node* head)
{
int xxor = 0;
struct Node* temp = head;
while (temp != NULL) {
xxor ^= temp->data;
temp = temp->next;
}
return xxor;
}
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 main()
{
struct Node* first = NULL;
push(&first, 1);
push(&first, 34);
push(&first, 10);
push(&first, 1);
push(&first, 34);
push(&first, 10);
if (!isPair(first)) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
return 0;
}
|
Java
class Node
{
int data;
Node next;
Node( int d)
{
data = d;
next = null ;
}
}
class SLL
{
static Node push(Node head, int data)
{
Node newNode = new Node(data);
newNode.next = head;
head = newNode;
return head;
}
static boolean isPair(Node head)
{
int xxor = 0 ;
Node temp = head;
while (temp != null )
{
xxor ^= temp.data;
temp = temp.next;
}
return xxor != 0 ;
}
public static void main(String[] args)
{
Node head = null ;
head = push(head, 1 );
head = push(head, 34 );
head = push(head, 10 );
head = push(head, 1 );
head = push(head, 34 );
head = push(head, 10 );
if (!isPair(head))
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
}
|
Python3
class Node:
def __init__( self ):
self .data = 0
self . next = None
def isPair( head):
xxor = 0
temp = head
while (temp ! = None ) :
xxor = xxor ^ temp.data
temp = temp. next
return xxor
def push( head_ref, new_data):
new_node = Node()
new_node.data = new_data
new_node. next = (head_ref)
(head_ref) = new_node
return head_ref
first = None
first = push(first, 1 )
first = push(first, 34 )
first = push(first, 10 )
first = push(first, 1 )
first = push(first, 34 )
first = push(first, 10 )
if ( not isPair(first)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
public class Node
{
public int data;
public Node next;
public Node( int d)
{
data = d;
next = null ;
}
}
public class SLL
{
static Node push(Node head, int data)
{
Node newNode = new Node(data);
newNode.next = head;
head = newNode;
return head;
}
static Boolean isPair(Node head)
{
int xxor = 0;
Node temp = head;
while (temp != null )
{
xxor ^= temp.data;
temp = temp.next;
}
return xxor != 0;
}
public static void Main(String[] args)
{
Node head = null ;
head = push(head, 1);
head = push(head, 34);
head = push(head, 10);
head = push(head, 1);
head = push(head, 34);
head = push(head, 10);
if (!isPair(head))
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
|
Javascript
<script>
class Node {
constructor(d) {
this .data = d;
this .next = null ;
}
}
function push(head, data) {
var newNode = new Node(data);
newNode.next = head;
head = newNode;
return head;
}
function isPair(head) {
var xxor = 0;
var temp = head;
while (temp != null ) {
xxor ^= temp.data;
temp = temp.next;
}
return xxor != 0;
}
var head = null ;
head = push(head, 1);
head = push(head, 34);
head = push(head, 10);
head = push(head, 1);
head = push(head, 34);
head = push(head, 10);
if (!isPair(head)) {
document.write( "Yes" );
} else {
document.write( "No" );
}
</script>
|
Time Complexity: O(n), where n is the number of nodes in the linked list
Auxiliary Space: O(1)
Method-2(Using Map)
We will traverse the complete linked list from left to right and store all elements in map with their frequency.
After completely traversing the linked list, we will find an element in the map which frequency is not even if any element is found we print “No” otherwise print “Yes”.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
Node( int value){
this ->data = value;
this ->next = NULL;
}
};
bool isPair(Node* head)
{
unordered_map< int , int > mp;
while (head != NULL){
mp[head->data]++;
head = head->next;
}
for ( auto i: mp)
if (i.second % 2 == 1) return false ;
return true ;
}
int main()
{
struct Node* head = new Node(10);
head->next = new Node(34);
head->next->next = new Node(1);
head->next->next->next = new Node(10);
head->next->next->next->next = new Node(34);
head->next->next->next->next->next = new Node(1);
if (isPair(head))
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
|
Java
import java.util.*;
class Node {
int data;
Node next;
Node( int value){
this .data = value;
this .next = null ;
}
}
class Main
{
static boolean isPair(Node head)
{
Map<Integer, Integer> mp = new HashMap<>();
while (head != null ){
mp.put(head.data, mp.getOrDefault(head.data, 0 ) + 1 );
head = head.next;
}
for (Map.Entry<Integer, Integer> entry : mp.entrySet())
if (entry.getValue() % 2 == 1 ) return false ;
return true ;
}
public static void main(String args[])
{
Node head = new Node( 10 );
head.next = new Node( 34 );
head.next.next = new Node( 1 );
head.next.next.next = new Node( 10 );
head.next.next.next.next = new Node( 34 );
head.next.next.next.next.next = new Node( 1 );
if (isPair(head))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python
class Node:
def __init__( self , value):
self .data = value
self . next = None
def isPair(head):
mp = {}
while (head is not None ):
if (mp.get(head.data) is not None ):
mp[head.data] + = 1
else :
mp[head.data] = 1
head = head. next
for x, y in mp.items():
if (y % 2 = = 1 ):
return False
return True
head = Node( 10 )
head. next = Node( 34 )
head. next . next = Node( 1 )
head. next . next . next = Node( 10 )
head. next . next . next . next = Node( 34 )
head. next . next . next . next . next = Node( 1 )
if (isPair(head) is True ):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
public class Node {
public int data;
public Node next;
public Node( int value)
{
this .data = value;
this .next = null ;
}
};
public class GFG
{
public static bool isPair(Node head)
{
Dictionary< int , int > mp
= new Dictionary< int , int >();
while (head != null ) {
if (mp.ContainsKey(head.data)) {
mp[head.data]++;
}
else {
mp.Add(head.data, 1);
}
head = head.next;
}
foreach (KeyValuePair< int , int > i in mp)
{
if (i.Value % 2 == 1) {
return false ;
}
}
return true ;
}
public static void Main()
{
Node head = new Node(10);
head.next = new Node(34);
head.next.next = new Node(1);
head.next.next.next = new Node(10);
head.next.next.next.next = new Node(34);
head.next.next.next.next.next = new Node(1);
if (isPair(head)) {
Console.WriteLine( "Yes" );
}
else {
Console.WriteLine( "No" );
}
}
}
|
Javascript
class Node{
constructor(value){
this .data = value;
this .next = null ;
}
}
function isPair(head){
let mp = new Map();
while (head != null ){
if (mp.has(head.data)){
mp.set(head.data, mp.get(head.data) + 1);
} else {
mp.set(head.data, 1);
}
head = head.next;
}
mp.forEach( function (value, key){
if (value % 2 == 1) return false ;
})
return true ;
}
let head = new Node(10);
head.next = new Node(34);
head.next.next = new Node(1);
head.next.next.next = new Node(10);
head.next.next.next.next = new Node(34);
head.next.next.next.next.next = new Node(1);
if (isPair(head))
console.log( "Yes" );
else
console.log( "No" )
|
Time Complexity: O(N) where N is the number of elements in given Linked List.
Auxiliary Space: O(N) due to map data structure.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!