-
-
Notifications
You must be signed in to change notification settings - Fork 49k
Add XOR Linked List implementation with doctests #13698
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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): | ||
| self.value = value | ||
| self.both: int = 0 # XOR of prev and next node ids | ||
|
|
||
|
|
||
| class XORLinkedList: | ||
| def __init__(self): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: |
||
| self.head: Optional[Node] = None | ||
|
Check failure on line 27 in data_structures/linked_list/xor_linked_list.py
|
||
| self.tail: Optional[Node] = None | ||
|
Check failure on line 28 in data_structures/linked_list/xor_linked_list.py
|
||
| 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
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: Please provide descriptive name for the parameter: |
||
| 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() | ||
There was a problem hiding this comment.
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: