forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxor_linked_list.py
More file actions
58 lines (46 loc) · 1.48 KB
/
xor_linked_list.py
File metadata and controls
58 lines (46 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""
XOR Linked List implementation
A memory-efficient doubly linked list using XOR of node addresses.
Each node stores one pointer that is the XOR of previous and next node addresses.
Example:
>>> xor_list = XORLinkedList()
>>> xor_list.insert(10)
>>> xor_list.insert(20)
>>> xor_list.insert(30)
>>> xor_list.to_list()
[10, 20, 30]
"""
from typing import Optional
class Node:
def __init__(self, value: int):
self.value = value
self.both: int = 0 # XOR of prev and next node ids
class XORLinkedList:
def __init__(self):
self.head: Optional[Node] = None
self.tail: Optional[Node] = None
self._nodes = {} # id → node map to simulate pointer references
def _xor(self, a: Optional[Node], b: Optional[Node]) -> int:
return (id(a) if a else 0) ^ (id(b) if b else 0)
def insert(self, value: int) -> None:
node = Node(value)
self._nodes[id(node)] = node
if self.head is None:
self.head = self.tail = node
else:
node.both = id(self.tail)
self.tail.both ^= id(node)
self.tail = node
def to_list(self) -> list[int]:
result = []
prev_id = 0
current = self.head
while current:
result.append(current.value)
next_id = prev_id ^ current.both
prev_id = id(current)
current = self._nodes.get(next_id)
return result
if __name__ == "__main__":
import doctest
doctest.testmod()