Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions data_structures/linked_list/xor_linked_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: __init__. If the function does not return a value, please provide the type hint as: def function() -> None:

self.value = value
self.both: int = 0 # XOR of prev and next node ids


class XORLinkedList:
def __init__(self):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: __init__. If the function does not return a value, please provide the type hint as: def function() -> None:

self.head: Optional[Node] = None

Check failure on line 27 in data_structures/linked_list/xor_linked_list.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP045)

data_structures/linked_list/xor_linked_list.py:27:20: UP045 Use `X | None` for type annotations

Check failure on line 27 in data_structures/linked_list/xor_linked_list.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP045)

data_structures/linked_list/xor_linked_list.py:27:20: UP045 Use `X | None` for type annotations

Check failure on line 27 in data_structures/linked_list/xor_linked_list.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP045)

data_structures/linked_list/xor_linked_list.py:27:20: UP045 Use `X | None` for type annotations

Check failure on line 27 in data_structures/linked_list/xor_linked_list.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP045)

data_structures/linked_list/xor_linked_list.py:27:20: UP045 Use `X | None` for type annotations
self.tail: Optional[Node] = None

Check failure on line 28 in data_structures/linked_list/xor_linked_list.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP045)

data_structures/linked_list/xor_linked_list.py:28:20: UP045 Use `X | None` for type annotations

Check failure on line 28 in data_structures/linked_list/xor_linked_list.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP045)

data_structures/linked_list/xor_linked_list.py:28:20: UP045 Use `X | None` for type annotations

Check failure on line 28 in data_structures/linked_list/xor_linked_list.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP045)

data_structures/linked_list/xor_linked_list.py:28:20: UP045 Use `X | None` for type annotations

Check failure on line 28 in data_structures/linked_list/xor_linked_list.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP045)

data_structures/linked_list/xor_linked_list.py:28:20: UP045 Use `X | None` for type annotations
self._nodes = {} # id → node map to simulate pointer references

def _xor(self, a: Optional[Node], b: Optional[Node]) -> int:

Check failure on line 31 in data_structures/linked_list/xor_linked_list.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP045)

data_structures/linked_list/xor_linked_list.py:31:42: UP045 Use `X | None` for type annotations

Check failure on line 31 in data_structures/linked_list/xor_linked_list.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP045)

data_structures/linked_list/xor_linked_list.py:31:23: UP045 Use `X | None` for type annotations

Check failure on line 31 in data_structures/linked_list/xor_linked_list.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP045)

data_structures/linked_list/xor_linked_list.py:31:42: UP045 Use `X | None` for type annotations

Check failure on line 31 in data_structures/linked_list/xor_linked_list.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP045)

data_structures/linked_list/xor_linked_list.py:31:23: UP045 Use `X | None` for type annotations

Check failure on line 31 in data_structures/linked_list/xor_linked_list.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP045)

data_structures/linked_list/xor_linked_list.py:31:42: UP045 Use `X | None` for type annotations

Check failure on line 31 in data_structures/linked_list/xor_linked_list.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP045)

data_structures/linked_list/xor_linked_list.py:31:23: UP045 Use `X | None` for type annotations

Check failure on line 31 in data_structures/linked_list/xor_linked_list.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP045)

data_structures/linked_list/xor_linked_list.py:31:42: UP045 Use `X | None` for type annotations

Check failure on line 31 in data_structures/linked_list/xor_linked_list.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP045)

data_structures/linked_list/xor_linked_list.py:31:23: UP045 Use `X | None` for type annotations

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: a

Please provide descriptive name for the parameter: b

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()
Loading