- XOR Linked List – A Memory Efficient Doubly Linked List | Set 1
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
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:
Before Conversion : 1 2 3 4 After Conversion : 1 2 3 4
- Convert singly linked list into circular linked list
- Difference between Singly linked list and Doubly linked list
- QuickSort on Singly Linked List
- Sum of the nodes of a Singly Linked List
- Implement a stack using singly linked list
- Alternate Odd and Even Nodes in a Singly Linked List
- Binary Search on Singly Linked List
- Product of the nodes of a Singly Linked List
- Insertion Sort for Singly Linked List
- Circular Singly Linked List | Insertion
- Alternating split of a given Singly Linked List | Set 1
- Find middle of singly linked list Recursively
- Difference between a Static Queue and a Singly Linked List
- Count of Prime Nodes of a Singly Linked List
- Find the common nodes in two singly linked list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.