Convert Singly Linked List to XOR Linked List

Prerequisite:

An XOR linked list is a memory efficient doubly linked list in which the next pointer of every node stores the XOR of previous and next node’s address.

Given a singly linked list, the task is to convert the given singly list to a XOR linked list.



Approach: Since in XOR linked list each next pointer stores the XOR of prev and next nodes’s address. So the idea is to traverse the given singly linked list and keep track of the previous node in a pointer say prev.

Now, while traversing the list, change the next pointer of every node as:

current -> next = XOR(prev, current->next) 

Printing the XOR linked list:
While printing XOR linked list we have to find the exact address of the next node every time. As we have seen above that the next pointer of every node stores the XOR value of prev and next node’s address. Therefore, the next node’s address can be obtained by finding XOR of prev and next pointer of current node in the XOR linked list.

So, to print the XOR linked list, traverse it by maintaining a prev pointer which stores the address of the previous node and to find the next node, calculate XOR of prev with next of current node.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to Convert a Singly Linked
// List to XOR Linked List
  
#include <bits/stdc++.h>
  
using namespace std;
  
// Linked List node
struct Node {
    int data;
    struct Node* next;
};
  
// Utiltity function to create new node
Node* newNode(int data)
{
    Node* temp = new Node;
    temp->data = data;
    temp->next = NULL;
  
    return temp;
}
  
// Print singly linked list before conversion
void print(Node* head)
{
    while (head) {
  
        // print current node
        cout << head->data << " ";
        head = head->next;
    }
  
    cout << endl;
}
  
// Function to find XORed value of
// the node addresses
Node* XOR(Node* a, Node* b)
{
    return (Node*)((uintptr_t)(a) ^ (uintptr_t)(b));
}
  
// Function to convert singly linked
// list to XOR linked list
void convert(Node* head)
{
    Node* curr = head;
    Node* prev = NULL;
    Node* next = curr->next;
  
    while (curr) {
  
        // store curr->next in next
        next = curr->next;
  
        // cahnge curr->next to XOR of prev and next
        curr->next = XOR(prev, next);
  
        // prev wil change to curr for next iteration
        prev = curr;
  
        // curr is now pointing to next for next iteration
        curr = next;
    }
}
  
// Fucntion to print XORed liked list
void printXOR(Node* head)
{
    Node* curr = head;
    Node* prev = NULL;
  
    while (curr) {
  
        // print current node
        cout << curr->data << " ";
  
        Node* temp = curr;
  
        /* compute curr as prev^curr->next as
           it is previously set as prev^curr->next so
           this time curr would be prev^prev^curr->next 
           which is curr->next */
        curr = XOR(prev, curr->next);
  
        prev = temp;
    }
  
    cout << endl;
}
  
// Driver Code
int main()
{
    // Create following singly linked list
    // 1->2->3->4
    Node* head = newNode(1);
    head->next = newNode(2);
    head->next->next = newNode(3);
    head->next->next->next = newNode(4);
  
    cout << "Before Conversion : " << endl;
    print(head);
  
    convert(head);
    cout << "After Conversion : " << endl;
    printXOR(head);
  
    return 0;
}

chevron_right


Output:

Before Conversion : 
1 2 3 4 
After Conversion : 
1 2 3 4


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.