Repository navigation
Expand file tree
/
Copy pathcircularly_linked_list.py
More file actions
100 lines (78 loc) · 3.18 KB
/
Copy pathcircularly_linked_list.py
File metadata and controls
100 lines (78 loc) · 3.18 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from src.data_structures.singly_linked_list_node import SinglyLinkedListNode
class CircularlyLinkedList[T]:
"""Circularly Linked List.
In the last node of a list, the link field often contains a null reference, a special value used to
indicate the lack of further nodes. A less common convention is to make it point to the first node of
the list; in that case the list is said to be 'circular' or 'circularly linked'; otherwise it is said
to be 'open' or 'linear'. It is a list where the last pointer points to the first node.
This is more of an example. It can be optimized in a variety of ways depending on its intended usage.
"""
def __init__(self, node: SinglyLinkedListNode[T] | None = None):
self._head: SinglyLinkedListNode[T] | None = node
if node:
node.next = self._head
@property
def head(self) -> SinglyLinkedListNode[T] | None:
return self._head
@head.setter
def head(self, node: SinglyLinkedListNode[T] | None):
self._head = node
if node:
node.next = self._head
def all_values(self) -> list[T]:
"""Return a list of every value in the list, starting from head and stopping once head is seen again."""
values = []
node = self.head
while node:
if node.data:
values.append(node.data)
node = node.next
if node == self.head:
break
return values
def append(self, node: SinglyLinkedListNode[T]):
"""Insert node just before head, so the list stays circular with head as the last-linked node."""
if not self.head:
self.head = node
node.next = self.head
return
previous_node = self.head
while previous_node and previous_node.next != self.head:
previous_node = previous_node.next
if not previous_node:
return
next_node = previous_node.next
previous_node.next = node
node.next = next_node
def remove(self, index: int):
"""Remove the node at index, walking the ring until either it or head is reached again."""
previous_node = self.head
if not previous_node:
raise IndexError("List is empty")
if previous_node.next == self.head:
self.head = None
return
if index == 0 and self.head:
previous_node.next = self.head.next
self.head = previous_node.next
return
list_index = 1
while previous_node and previous_node.next is not self.head and list_index < index:
previous_node = previous_node.next
list_index += 1
if list_index == index and previous_node and previous_node.next:
next_node = previous_node.next
previous_node.next = next_node.next
else:
raise IndexError
def size(self) -> int:
"""Return the number of nodes in the ring."""
count = 0
if not self.head:
return count
count += 1
node = self.head.next
while node and node != self.head:
count += 1
node = node.next
return count