- 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
- Alternate Odd and Even Nodes in 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
- Binary Search on Singly Linked List
- Product of the nodes of a Singly Linked List
- Implement a stack using singly linked list
- Difference between a Static Queue and a Singly Linked List
- Delete all Prime Nodes from a Singly Linked List
- Select a Random Node from a Singly Linked List
- Delete all Non-Prime Nodes from a 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 email@example.com. 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.