Skip to content
Related Articles

Related Articles

Improve Article

XOR Linked List – Reversal of a List

  • Last Updated : 20 Jan, 2021
Geek Week

Given a XOR linked list, the task is to reverse the XOR linked list.

Examples:

Input: 4 <–> 7 <–> 9 <–> 7
Output: 7 <–> 9 <–> 7 <–> 4
Explanation:
Reversing the linked list modifies the XOR linked list to 7 <–> 9 <–> 7 <–> 4.

Input: 2 <-> 5 <-> 7 <-> 4
Output: 4 <-> 7 <-> 5 <-> 2
Explanation:
Reversing the linked list modifies the XOR linked list to 4 <-> 7 <-> 5 <-> 2.

Approach: XOR linked listconsists of a single pointer, which is the only pointer needed to traverse the XOR linked list in both directions. Therefore, the idea to solve this problem is only by making the last node of the XOR linked list its Head Node. Follow the steps below to solve the problem:



  1. Initialize a pointer variable, say curr, to pointto the current node being traversed.
  2. Store the current head pointer in the curr variable.
  3. If curr is equal to NULL, then return NULL.
  4. Otherwise, traverse upto the last node and make it the head of the XOR linked list.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include<bits/stdc++.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
    Node* nxp;
};
 
// Function to find the XOR of two nodes
Node* XOR(Node* a, Node* b)
{
    return (Node*)((uintptr_t)(a) ^ (uintptr_t)(b));
}
 
// Function to insert a node with
// given value at begining position
Node* insert(Node** head, int value)
{
 
    // If XOR linked list is empty
    if (*head == NULL)
    {
 
        // Initialize a new Node
        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
        Node* curr = *head;
 
        // Stores the address
        // of previous node
        Node* prev = NULL;
 
        // Initialize a new Node
        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 elements of
// the XOR Linked List
void printList(Node** head)
{
 
    // Stores XOR pointer
    // in current node
    Node* curr = *head;
 
    // Stores XOR pointer of
    // in previous Node
    Node* prev = NULL;
 
    // Stores XOR pointer of
    // in next node
    Node* next;
 
    // Traverse XOR linked list
    while (curr != NULL)
    {
 
        // Print current node
        cout<<curr->data<<" ";
 
        // Forward traversal
        next = XOR(prev, curr->nxp);
 
        // Update prev
        prev = curr;
 
        // Update curr
        curr = next;
    }
    cout << endl;
}
 
// Function to reverse the XOR linked list
Node* reverse(Node** head)
{
 
    // Stores XOR pointer
    // in current node
    Node* curr = *head;
    if (curr == NULL)
        return NULL;
    else
    {
 
        // Stores XOR pointer of
        // in previous Node
        Node* prev = NULL;
 
        // Stores XOR pointer of
        // in next node
        Node* next;
        while (XOR(prev, curr->nxp) != NULL)
        {
 
            // Forward traversal
            next = XOR(prev, curr->nxp);
 
            // Update prev
            prev = curr;
 
            // Update curr
            curr = next;
        }
 
        // Update the head pointer
        *head = curr;
        return *head;
    }
}
 
// Driver Code
int main()
{
    /* Create following XOR Linked List
    head-->40<-->30<-->20<-->10 */
    Node* head = NULL;
    insert(&head, 10);
    insert(&head, 20);
    insert(&head, 30);
    insert(&head, 40);
 
    /* Reverse the XOR Linked List to give
    head-->10<-->20<-->30<-->40 */
    cout << "XOR linked list: ";
    printList(&head);
    reverse(&head);
    cout << "Reversed XOR linked list: ";
    printList(&head);
 
    return 0;
}
 
// This code is contributed by rutvik_56.

C




// C program for 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 begining 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 elements of
// the XOR Linked List
void printList(struct Node** head)
{
 
    // 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;
 
    // Traverse XOR linked list
    while (curr != NULL) {
 
        // Print current node
        printf("%d ", curr->data);
 
        // Forward traversal
        next = XOR(prev, curr->nxp);
 
        // Update prev
        prev = curr;
 
        // Update curr
        curr = next;
    }
    printf("\n");
}
 
// Function to reverse the XOR linked list
struct Node* reverse(struct Node** head)
{
 
    // Stores XOR pointer
    // in current node
    struct Node* curr = *head;
    if (curr == NULL)
        return NULL;
    else {
 
        // Stores XOR pointer of
        // in previous Node
        struct Node* prev = NULL;
 
        // Stores XOR pointer of
        // in next node
        struct Node* next;
        while (XOR(prev, curr->nxp) != NULL) {
 
            // Forward traversal
            next = XOR(prev, curr->nxp);
 
            // Update prev
            prev = curr;
 
            // Update curr
            curr = next;
        }
 
        // Update the head pointer
        *head = curr;
        return *head;
    }
}
 
// Driver Code
int main()
{
    /* Create following XOR Linked List
    head-->40<-->30<-->20<-->10 */
    struct Node* head = NULL;
    insert(&head, 10);
    insert(&head, 20);
    insert(&head, 30);
    insert(&head, 40);
 
    /* Reverse the XOR Linked List to give
    head-->10<-->20<-->30<-->40 */
    printf("XOR linked list: ");
    printList(&head);
    reverse(&head);
    printf("Reversed XOR linked list: ");
    printList(&head);
 
    return (0);
}

 
 

Output: 
XOR linked list: 40 30 20 10 
Reversed XOR linked list: 10 20 30 40

 

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :