Check if elements of Linked List are present in pair - GeeksforGeeks

29 Jun, 2021
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++
Java
Python3
C#
Javascript
// C++ program to check if elements of// linked lists are present in pair#include <bits/stdc++.h> using namespace std; // A linked list nodestruct Node {    int data;    struct Node* next;}; // Function to check if elements of// linked list are present in pairbool isPair(struct Node* head){     int xxor = 0;     struct Node* temp = head;     while (temp != NULL) {        xxor ^= temp->data;        temp = temp->next;    }     return xxor;} // Function to add a node at the// beginning of Linked Listvoid push(struct Node** head_ref, int new_data){    /* allocate node */    struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));     /* put in the data */    new_node->data = new_data;     /* link the old list off the new node */    new_node->next = (*head_ref);     /* move the head to point to the new node */    (*head_ref) = new_node;} // Driver program to test above functionint main(){    struct Node* first = NULL;     /* First constructed linked list is:    10 -> 34 -> 1 -> 10 -> 34 -> 1 */    push(&first, 1);    push(&first, 34);    push(&first, 10);    push(&first, 1);    push(&first, 34);    push(&first, 10);     // Calling function to check pair elements    if (!isPair(first)) {        cout << "Yes" << endl;    }    else {        cout << "No" << endl;    }     return 0;}
// Java program to check if elements of// linked lists are present in pair // Node Classclass Node{    int data;    Node next;     // Constructor to create a new node    Node(int d)    {        data = d;        next = null;    }} class SLL{     // function to insert a node at the beginning    // of the Singly Linked List    static Node push(Node head, int data)    {        Node newNode = new Node(data);        newNode.next = head;        head = newNode;        return head;    }     // Function to check if elements of    // linked list are present in pair    static boolean isPair(Node head)    {        int xxor = 0;        Node temp = head;        while (temp != null)        {            xxor ^= temp.data;            temp = temp.next;        }        return xxor != 0;    }     // Driver code    public static void main(String[] args)    {        Node head = null;         // First constructed linked list        // 10 -> 34 -> 1 -> 10 -> 34 -> 1         head = push(head, 1);        head = push(head, 34);        head = push(head, 10);        head = push(head, 1);        head = push(head, 34);        head = push(head, 10);         // Calling function to check pair elements        if (!isPair(head))        {            System.out.println("Yes");        }        else        {            System.out.println("No");        }    }} // This code is contributed by Vivekkumar Singh
# Python3 program to check if elements of# linked lists are present in pair # A linked list nodeclass Node:    def __init__(self):        self.data = 0        self.next = None # Function to check if elements of# linked list are present in pairdef isPair( head):    xxor = 0    temp = head     while (temp != None) :        xxor = xxor ^ temp.data        temp = temp.next     return xxor # Function to add a node at the# beginning of Linked Listdef push( head_ref, new_data):     # allocate node    new_node = Node()     # put in the data    new_node.data = new_data     # link the old list off the new node    new_node.next = (head_ref)     # move the head to point to the new node    (head_ref) = new_node         return head_ref # Driver code first = None # First constructed linked list is:# 10 . 34 . 1 . 10 . 34 . 1first = push(first, 1)first = push(first, 34)first = push(first, 10)first = push(first, 1)first = push(first, 34)first = push(first, 10) # Calling function to check pair elementsif (not isPair(first)):    print( "Yes" )     else :    print( "No" )     # This code is contributed by Arnab Kundu
// C# program to check if elements of// linked lists are present in pairusing System; // Node Classpublic class Node{    public int data;    public Node next;     // Constructor to create a new node    public Node(int d)    {        data = d;        next = null;    }} public class SLL{     // function to insert a node at the beginning    // of the Singly Linked List    static Node push(Node head, int data)    {        Node newNode = new Node(data);        newNode.next = head;        head = newNode;        return head;    }     // Function to check if elements of    // linked list are present in pair    static Boolean isPair(Node head)    {        int xxor = 0;        Node temp = head;        while (temp != null)        {            xxor ^= temp.data;            temp = temp.next;        }        return xxor != 0;    }     // Driver code    public static void Main(String[] args)    {        Node head = null;         // First constructed linked list        // 10 -> 34 -> 1 -> 10 -> 34 -> 1         head = push(head, 1);        head = push(head, 34);        head = push(head, 10);        head = push(head, 1);        head = push(head, 34);        head = push(head, 10);         // Calling function to check pair elements        if (!isPair(head))        {            Console.WriteLine("Yes");        }        else        {            Console.WriteLine("No");        }    }} // This code is contributed by Rajput-Ji
<script>       // JavaScript program to check if elements of      // linked lists are present in pair      // Node Class      class Node {        // Constructor to create a new node        constructor(d) {          this.data = d;          this.next = null;        }      }       // function to insert a node at the beginning      // of the Singly Linked List      function push(head, data) {        var newNode = new Node(data);        newNode.next = head;        head = newNode;        return head;      }       // Function to check if elements of      // linked list are present in pair      function isPair(head) {        var xxor = 0;        var temp = head;        while (temp != null) {          xxor ^= temp.data;          temp = temp.next;        }        return xxor != 0;      }       // Driver code      var head = null;       // First constructed linked list      // 10 -> 34 -> 1 -> 10 -> 34 -> 1       head = push(head, 1);      head = push(head, 34);      head = push(head, 10);      head = push(head, 1);      head = push(head, 34);      head = push(head, 10);       // Calling function to check pair elements      if (!isPair(head)) {        document.write("Yes");      } else {        document.write("No");      }       </script>
Yes
 
Vivekkumar Singh
Rajput-Ji
Akanksha_Rai
andrew1234
rdtank
Bitwise-XOR
Bit Magic
Linked List
Linked List
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Set, Clear and Toggle a given bit of a number in C
Swap two nibbles in a byte
Write an Efficient Method to Check if a Number is Multiple of 3
Highest power of 2 less than or equal to given number
Swap bits in a given number
Linked List | Set 1 (Introduction)
Linked List | Set 2 (Inserting a node)
Reverse a linked list
Stack Data Structure (Introduction and Program)
Linked List | Set 3 (Deleting a node)