Yuri Vishnevsky

February 2025
XOR Doubly-Linked List (via)
TIL

Neat idea: you can make a doubly-linked list that only needs to store a single pointer – the xor of the prev and next pointers – to allow traversal of the list from either direction.

Looks like author never heard about single pointer double linked list. That could save him 2 pointers per each node.

The idea: for simplicity taking list. single pointer saved in the node is xor of next and previous nodes. When moving forward we xor current ptr with prev. Moving backward xor with next to get prev. That it. How do we get prev/next for xor? The only way to get in the middle of the list is using iterator. Which can hold one extra pointer. For the first/last nodes extra pointer is NULL.

– numba888 on Hacker News

Update: There is a Wikipedia entry for this idea: XOR linked list

Back to top