XOR Linked List | Practice | GeeksforGeeks

An ordinary Doubly Linked List requires space for two address fields to store the addresses of previous and next nodes. A memory efficient version of Doubly Linked List can be created using only one space for address field with every node. This memory efficient Doubly Linked List is called XOR Linked List or Memory Efficient as the list uses bit-wise XOR operation to save space for one address.
Given stream of data of size N for the linked list, your task is to complete the function insert() and printList(). The insert() function pushes (or inserts at beginning) the given data in the linked list and the printList() function returns the linked list as a list.
Note: There is an utility function XOR() that takes two Node pointer to get the bit-wise XOR of the two Node pointer. Use this function to get the XOR of the two pointers. 
Example 1:
Input:
LinkedList: 9<->5<->4<->7<->3<->10
Output:
10 3 7 4 5 9
9 5 4 7 3 10

Example 2:
Input:
LinkedList: 58<->96<->31
Output:
31 96 58
58 96 31

Your Task:
The task is to complete the function insert() and printList() as required.
Note: The driver code prints the returned list twice, once forward and once backward.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 100
 
0
visitant5 days ago
struct Node *insert(struct Node *head, int data) {
    // Code here
    struct Node* temp = new Node(data);
    if(head == NULL)
    {
        temp->npx = XOR(temp->npx,XOR(head,temp));
    }
    else
    {
        temp->npx = XOR(temp->npx,XOR(head,temp));
    }
    
    return temp;
    
}

vector<int> printList(struct Node *head) {
    // Code here
    struct Node* temp = head;
    vector <int> vList;
    while(temp != NULL)
    {
        vList.push_back(temp->data);
        temp = XOR(temp->npx, temp);
    }
    return vList;
}
0
awektoppo3 weeks ago
struct Node *insert(struct Node *head, int data) {   // Code here   Node *tmp = new Node(data);   tmp->npx = head;         if(head){       head->npx = XOR(tmp,head->npx);
   }   return tmp;}
vector<int> printList(struct Node *head) {   // Code here   vector<int > v;   Node *curr = head;   Node *prev =NULL;   Node *next;      while(curr){       v.push_back(curr->data);       next = XOR(prev,curr->npx);       prev =curr;       curr = next;                 }   return v;}
0
thechallenger656 months ago
struct Node* insert(struct Node *head, int data){   struct Node *new_node = new Node(data);   new_node->npx = XOR(head,NULL);   if (head!= NULL)   {   struct Node *next = XOR(head->npx, NULL);   head->npx = XOR(new_node, next);   }   head = new_node;}vector<int> printList (struct Node *head){   struct Node *curr = head;   struct Node *prev = NULL;   struct Node *next;   vector<int> res;   while (curr != NULL)   {   res.push_back(curr->data);   next = XOR(prev, curr->npx);   prev = curr;   curr = next;   }   return res;}
0
thechallenger656 months ago
struct Node* insert(struct Node *head, int data){   struct Node *new_node = new Node(data);   new_node->npx = XOR(head,NULL);   if (head!= NULL)   {   struct Node *next = XOR(head->npx, NULL);   head->npx = XOR(new_node, next);   }   head = new_node;}vector<int> printList (struct Node *head){   struct Node *curr = head;   struct Node *prev = NULL;   struct Node *next;   vector<int> res;   while (curr != NULL)   {   res.push_back(curr->data);   next = XOR(prev, curr->npx);   prev = curr;   curr = next;   }   return res;}
0
deespace4767
This comment was deleted.
0
Aravinth S11 months ago
Aravinth S
struct Node* insert(struct Node *head, int data){    struct Node *new_node = new Node(data);    new_node->npx = XOR(head,NULL);     if (head!= NULL)     {          struct Node *next = XOR(head->npx, NULL);         head->npx = XOR(new_node, next);     }     head = new_node; }
vector<int> printList (struct Node *head){    struct Node *curr = head;     struct Node *prev = NULL;     struct Node *next;     vector<int> res;    while (curr != NULL)     {         res.push_back(curr->data);        next = XOR(prev, curr->npx);         prev = curr;         curr = next;     }     return res;}
0
Aravinth S
This comment was deleted.
0
Manan Varma1 year ago
Manan Varma
C++ Solution:
https://ide.geeksforgeeks.o...
0
Aditya Aggarwal1 year ago
Aditya Aggarwal
struct Node* insert(struct Node *head, int data){if(head==NULL)  {    head=new Node(data);     head->npx=XOR(NULL,head);    return head; }Node *q=new Node(data);    q->npx=XOR(head,q);    head=q;    return head;}vector<int> printList (struct Node *head){    vector<int>a;    Node *p=head;    while(p!=NULL){        a.push_back(p->data);        p=XOR(p,p->npx);    }    return a;}
0
Rahul Awari
This comment was deleted.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.