XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
Last Updated : 13 Feb, 2025
In this post, we're going to talk about how XOR linked lists are used to reduce the memory requirements of doubly-linked lists.
We know that each node in a doubly-linked list has two pointer fields which contain the addresses of the previous and next node. On the other hand, each node of the XOR linked list requires only a single pointer field, which doesnβt store the actual memory addresses but stores the bitwise XOR of addresses for its previous and next node.
In this section, we will discuss both ways in order to demonstrate how XOR representation of doubly linked list differs from ordinary representation of doubly linked list.
Below is the representation of a node structure for an XOR linked list:
Types of XOR Linked List:
There are two main types of XOR Linked List:
Singly Linked XOR List: A singly XOR linked list is a variation of the XOR linked list that uses the XOR operation to store the memory address of the next node in a singly linked list. In this type of list, each node stores the XOR of the memory address of the next node and the memory address of the current node.
Doubly Linked XOR List: A doubly XOR linked list is a variation of the XOR linked list that uses the XOR operation to store the memory addresses of the next and previous nodes in a doubly linked list. In this type of list, each node stores the XOR of the memory addresses of the next and previous nodes.
Traversal in XOR linked list:
Two types of traversal are possible in XOR linked list.
Forward Traversal
Backward Traversal:
Forward Traversal in XOR linked list:
When traversing the list forward, itβs important to always keep the memory address of the previous element. Address of previous element helps in calculating the address of the next element by the below formula:
address of next Node = (address of prev Node) ^ (both)
Here, "both" is the XOR of address of previous node and address of next node.
Below is the code snippet for forward traversal of the XOR linked list:
Backward Traversal in XOR linked list:
When traversing the list backward, itβs important to always keep the memory address of the next element. Address of next element helps in calculating the address of the previous element by the below formula:
address of previous Node = (address of next Node) ^ (both)
Here, "both" is the XOR of address of previous node and address of next node.
Below is the code snippet for backward traversal of the XOR linked list:
Basic Operations of XOR Linked list:
Insertion
Deletion
Insertion at Beginning in XOR Linked List:
Below is the steps for insert an element at beginning in XOR Linked List:
Create a new node , initialize the data and address to the (NULL ^ address of head)
Then check, If the list is empty, return with that node;
Otherwise, assign the XOR of the head node to the XOR(new_node address, XOR(head->both, nullptr))
Insertion at end in XOR Linked List:
Below is the steps for insert an element at end in XOR Linked List:
Create a new node , initialize the data and address to the (NULL ^ add. of tail)
Then check, If the list is empty, return with that node;
Otherwise, assign the XOR of the tail node to the XOR(XOR(tail->both, nullptr), new_node address)
Deletion at Beginning in XOR Linked List:
Below is the steps for delete an element at beginning in XOR Linked List:
Check if the head pointer is not null (i.e., the list is not empty).
Find the next node's address using XOR by performing XOR(head->both, nullptr)
Delete the current head node to free up the memory.and Update the head pointer to point to the calculated next node.
Deletion at End in XOR Linked List:
Below is the steps for delete an element at beginning in XOR Linked List:
Check if the tail pointer is not null (i.e., the list is not empty).
If the list is not empty:
Find the previous node's address using XOR by performing XOR(tail->both, nullptr). This gives you the previous node in the list.
Delete the current tail node to free up the memory.
Update the tail pointer to point to the calculated previous node.
Below is the implementation of the above approach:
Output
20 10 30 40
10 30 40
10 30
Time Complexity: O(n) Auxiliary Space: O(1)
Advantages and Disadvantages Of XOR Linked List:
Advantages:
XOR linked lists use less memory compared to traditional doubly linked lists. This is because they only need one "pointer" (the XOR of the previous and next pointers) instead of two separate pointers, which can save memory in applications where memory is a critical resource.
XOR linked lists can be traversed in both directions (forward and backward) without the need for an additional pointer to the previous node.
Insertion and deletion at both the head and tail of the list can be done in constant time (O(1)), just like in traditional singly linked lists. This makes them efficient for certain operations.
Disadvantages:
XOR linked lists are more complex to implement and maintain than traditional linked lists.
XOR linked lists are not a standard data structure in most programming languages and libraries.