Skip to content
Related Articles

Related Articles

XOR Linked List – Find the middle node

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 13 Oct, 2021
Improve Article
Save Article

Given an XOR linked list, the task is to find the middle node of the given XOR linked list.

Examples:

Input: 4 –> 7 –> 5 
Output:
Explanation: 
The middle node of the given XOR list is 7.

Input: 4 –> 7 –> 5 –> 1 
Output: 7 5 
Explanation: 
The two middle nodes of the XOR linked list with even number of nodes are 7 and 5. 
 

Approach: Follow the steps below to solve the problem:

  • Traverse to (Length / 2)th node of the Linked List.
  • If the number of nodes is found to be odd, then print (Length + 1) / 2 th node as the only middle node.
  • If the number of nodes is found to be even, then print both Length / 2 th node and (Length / 2) + 1 th node as the middle nodes.

Below is the implementation of the above approach:

C++




// C++ program to implement
// the above approach
 
#include <bits/stdc++.h>
#include <inttypes.h>
using namespace std;
 
// Structure of a node
// in XOR linked list
struct Node {
 
    // Stores data value
    // of a node
    int data;
 
    // Stores XOR of previous
    // pointer and next pointer
    struct Node* nxp;
};
 
// Function to find the XOR of two nodes
struct Node* XOR(struct Node* a, struct Node* b)
{
    return (struct Node*)((uintptr_t)(a) ^ (uintptr_t)(b));
}
 
// Function to insert a node with
// given value at given position
struct Node* insert(struct Node** head, int value)
{
 
    // If XOR linked list is empty
    if (*head == NULL) {
 
        // Initialize a new Node
        struct Node* node = new Node;
 
        // Stores data value in
        // the node
        node->data = value;
 
        // Stores XOR of previous
        // and next pointer
        node->nxp = XOR(NULL, NULL);
 
        // Update pointer of head node
        *head = node;
    }
 
    // If the XOR linked list
    // is not empty
    else {
 
        // Stores the address
        // of current node
        struct Node* curr = *head;
 
        // Stores the address
        // of previous node
        struct Node* prev = NULL;
 
        // Initialize a new Node
        struct Node* node
            = new Node();
 
        // Update curr node address
        curr->nxp = XOR(node, XOR(NULL, curr->nxp));
 
        // Update new node address
        node->nxp = XOR(NULL, curr);
 
        // Update head
        *head = node;
 
        // Update data value of
        // current node
        node->data = value;
    }
    return *head;
}
 
// Function to print the middle node
int printMiddle(struct Node** head, int len)
{
    int count = 0;
 
    // Stores XOR pointer
    // in current node
    struct Node* curr = *head;
 
    // Stores XOR pointer of
    // in previous Node
    struct Node* prev = NULL;
 
    // Stores XOR pointer of
    // in next node
    struct Node* next;
 
    int middle = (int)len / 2;
 
    // Traverse XOR linked list
    while (count != middle) {
 
        // Forward traversal
        next = XOR(prev, curr->nxp);
 
        // Update prev
        prev = curr;
 
        // Update curr
        curr = next;
 
        count++;
    }
 
    // If the length of the
    // linked list is odd
    if (len & 1) {
        cout << curr->data << " ";
    }
 
    // If the length of the
    // linked list is even
    else {
        cout << prev->data << " " << curr->data << " ";
    }
}
 
// Driver Code
int main()
{
    /* Create following XOR Linked List
    head --> 4 –> 7 –> 5 */
    struct Node* head = NULL;
    insert(&head, 4);
    insert(&head, 7);
    insert(&head, 5);
 
    printMiddle(&head, 3);
 
    return (0);
}

C




// C program to implement
// the above approach
 
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
 
// Structure of a node
// in XOR linked list
struct Node {
 
    // Stores data value
    // of a node
    int data;
 
    // Stores XOR of previous
    // pointer and next pointer
    struct Node* nxp;
};
 
// Function to find the XOR of two nodes
struct Node* XOR(struct Node* a,
                 struct Node* b)
{
    return (struct Node*)((uintptr_t)(a)
                          ^ (uintptr_t)(b));
}
 
// Function to insert a node with
// given value at given position
struct Node* insert(struct Node** head,
                    int value)
{
 
    // If XOR linked list is empty
    if (*head == NULL) {
 
        // Initialize a new Node
        struct Node* node
            = (struct Node*)malloc(
                sizeof(struct Node));
 
        // Stores data value in
        // the node
        node->data = value;
 
        // Stores XOR of previous
        // and next pointer
        node->nxp = XOR(NULL, NULL);
 
        // Update pointer of head node
        *head = node;
    }
 
    // If the XOR linked list
    // is not empty
    else {
 
        // Stores the address
        // of current node
        struct Node* curr = *head;
 
        // Stores the address
        // of previous node
        struct Node* prev = NULL;
 
        // Initialize a new Node
        struct Node* node
            = (struct Node*)malloc(
                sizeof(struct Node));
 
        // Update curr node address
        curr->nxp = XOR(
            node, XOR(NULL, curr->nxp));
 
        // Update new node address
        node->nxp = XOR(NULL, curr);
 
        // Update head
        *head = node;
 
        // Update data value of
        // current node
        node->data = value;
    }
    return *head;
}
 
// Function to print the middle node
int printMiddle(struct Node** head, int len)
{
    int count = 0;
 
    // Stores XOR pointer
    // in current node
    struct Node* curr = *head;
 
    // Stores XOR pointer of
    // in previous Node
    struct Node* prev = NULL;
 
    // Stores XOR pointer of
    // in next node
    struct Node* next;
 
    int middle = (int)len / 2;
 
    // Traverse XOR linked list
    while (count != middle) {
 
        // Forward traversal
        next = XOR(prev, curr->nxp);
 
        // Update prev
        prev = curr;
 
        // Update curr
        curr = next;
 
        count++;
    }
 
    // If the length of the
    // linked list is odd
    if (len & 1) {
        printf("%d", curr->data);
    }
 
    // If the length of the
    // linked list is even
    else {
        printf("%d %d", prev->data,
               curr->data);
    }
}
 
// Driver Code
int main()
{
    /* Create following XOR Linked List
    head --> 4 –> 7 –> 5 */
    struct Node* head = NULL;
    insert(&head, 4);
    insert(&head, 7);
    insert(&head, 5);
 
    printMiddle(&head, 3);
 
    return (0);
 
}

Output: 

7

 

Time Complexity: O(N) 
Auxiliary Space: O(1)

 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!