The memory-efficient doubly linked list uses only one pointer to traverse the list in both directions. This is achieved by using a bitwise XOR operator to store the front and rear pointer addresses. Rather than storing the actual memory addresses, each node stores the XOR address of the previous and next nodes.

**Doubly linked lists** data structure are a type of linked lists that can be traversed in both directions, i.e. forward (head to the last node) and backward direction (last node to head). The node of the doubly linked list has three fields: data, address of the next node, and address of the previous node.

The node of a Doubly linked list:

It occupies an extra space for the address of the previous node compared to a simple linked list node as the doubly linked list node contains both the addresses of the previous and the next node. This article will learn about the memory-efficient doubly linked list or XOR linked list, which has only one address or pointer per node using the property of bitwise XOR.

A node of an ordinary __doubly linked list__ contains two addresses: the address of the previous node and the address of the next node, but a node of the memory-efficient doubly linked list contains only one address, the Bitwise XOR of the previous and next address. It can be used as two addresses using an important property of bitwise XOR.

**An interesting property of bitwise XOR :**

`If X ^ Y = Z then Y ^ Z = X and X ^ Z = Y`

From the above property, if we have X and Y, we can compute Z. If we have Y and Z, we can compute X, and if we have X and Z, we can compute the value of Y.

We use this property in our doubly linked list for storing the address. Instead of keeping the next and previous addresses, we store the XOR of next and previous addresses.

- Address of the Next Node = Address of Previous Node ^ Address of the current Node.
- Address of the Previous Node = Address of Next Node ^ Address of the current Node.

So if we have a linked list like this:

Nodes of the memory-efficient linked list after using the property of XOR will look like this;

- Node n1: n1->next = NULL(0)
**XOR**address of (n2) - Node n2: n2->next = address of (n1)
**XOR**address of (n3) - Node n3: n3->next = address of (n2)
**XOR**address of (n4) - Node n4: n4->next = address of (n3)
**XOR**NULL(0)

We can traverse the list in both directions in the linked list without any extra previous pointer.

## Inserting a node

- We first create a new node.
- Make the next of the new nodes the NULL XOR address(head), i.e. the address of the previous node XOR address of the next node.
- Now, we update the addresses of the next nodes after the head node to the address of the previous node XOR address of the next node.
- Finally, we make the new node the head of the list.

## Printing the list

- We create a previous node pointing to NULL and a current node pointing to the head of the list.
- Now, keep repeating the below processes till the end of the list.
- Print the current->data.
- Now, we store the address of the next node as the address of the previous node XOR address of the next node(current->next).
- We update the previous node to the current node and the current node to the next node of the list.

Read about __Bitwise Operators in C__ here.