How exactly does a XOR Linked list work?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
A XOR linked list is a space-saving variation of a doubly linked list. Instead of storing separate prev and next pointers, each node stores one value equal to prev XOR next, and traversal works only because you already know one of the two addresses at each step.
The Core Identity
The entire trick depends on one XOR property:
If a node stores both = prev XOR next and you know prev, you can recover next:
Likewise, if you know next, you can recover prev.
That is why traversal always carries one extra piece of state: the address of the node you just came from.
Conceptual Layout
In a normal doubly linked list:
In a XOR linked list:
For the head node, the previous address is zero. For the tail node, the next address is zero. So the first node stores 0 XOR next, which is just next.
Low-Level Example in C
This structure is only practical in low-level languages that let you manipulate addresses directly. In C, uintptr_t is the usual integer type for pointer-sized arithmetic.
The important line is:
Given the previous node and the mixed address stored in both, you recover the next node.
Why People Rarely Use It
The memory saving is real in theory, but the tradeoff is usually not worth it in modern software.
Reasons it is uncommon:
- debugging is much harder
- pointer arithmetic is easy to get wrong
- garbage-collected languages do not expose raw addresses safely
- many tools expect ordinary pointers, not XOR-encoded ones
- the memory saved is often insignificant compared with the complexity added
A normal doubly linked list is easier to maintain, easier to profile, and much safer.
Insertion and Deletion Are Tricky
Traversal is the easy part. Updates are where XOR lists become awkward. To insert a node between left and right, you must recompute the both field of all affected nodes carefully. If one pointer is wrong, the entire structure becomes unreadable.
That fragility is a major reason XOR lists are mostly educational. They are good for understanding pointer representation and invariants, but poor as a default production choice.
Common Pitfalls
The biggest mistake is trying to implement XOR lists in a language where object addresses are not stable or not accessible. In Java, C#, Python, and JavaScript, this design is generally a bad fit.
Another mistake is assuming the XOR field can be inspected meaningfully in a debugger. It usually cannot, because it is not a valid standalone pointer.
A third problem is forgetting that you always need the previous node to compute the next one. Random access is not improved at all, and backward traversal still requires state.
Summary
- A XOR linked list stores
prev XOR nextinstead of two separate pointers. - Traversal works because knowing one neighbor lets you recover the other.
- The technique is mainly practical in low-level languages with raw pointer arithmetic.
- Insert and delete operations are harder than in a normal doubly linked list.
- In most real code, the small memory savings do not justify the complexity.
Related reading
- How exactly does tail recursion work?
- How external merge sort algorithm works?
- How hard is this graph problem?
- How is 2D bin packing achieved programmatically?
- How good can Nearest Neighbor, Naive Bayes and a Decision Tree classifier solve the given classification problem?
- How is a minimum bottleneck spanning tree different from a minimum spanning tree?
- How expensive is the lock statement?
- How expensive is the lock statement?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.