Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0c8cf8e

Browse files
Fix bug for data_structures/linked_list/doubly_linked_list_two.py (#12651)
* Fix bug for data_structures/linked_list/doubly_linked_list_two.py * Fix * Fix * Fix * Fix * Fix * Fix * Fix * Update doubly_linked_list_two.py * Update doubly_linked_list_two.py * Update doubly_linked_list_two.py * Update doubly_linked_list_two.py * Update doubly_linked_list_two.py * Update doubly_linked_list_two.py * Update doubly_linked_list_two.py * Update doubly_linked_list_two.py * Update doubly_linked_list_two.py * Update doubly_linked_list_two.py * Update doubly_linked_list_two.py * Update doubly_linked_list_two.py
1 parent baab802 commit 0c8cf8e

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

‎data_structures/linked_list/doubly_linked_list_two.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ def set_head(self, node: Node) -> None:
8181
self.insert_before_node(self.head, node)
8282

8383
def set_tail(self, node: Node) -> None:
84-
if self.head is None:
85-
self.set_head(node)
84+
if self.tail is None:
85+
self.head = node
86+
self.tail = node
8687
else:
8788
self.insert_after_node(self.tail, node)
8889

@@ -104,9 +105,7 @@ def insert_before_node(self, node: Node, node_to_insert: Node) -> None:
104105

105106
node.previous = node_to_insert
106107

107-
def insert_after_node(self, node: Node | None, node_to_insert: Node) -> None:
108-
assert node is not None
109-
108+
def insert_after_node(self, node: Node, node_to_insert: Node) -> None:
110109
node_to_insert.previous = node
111110
node_to_insert.next = node.next
112111

@@ -127,7 +126,7 @@ def insert_at_position(self, position: int, value: int) -> None:
127126
return
128127
current_position += 1
129128
node = node.next
130-
self.insert_after_node(self.tail, new_node)
129+
self.set_tail(new_node)
131130

132131
def get_node(self, item: int) -> Node:
133132
node = self.head
@@ -237,6 +236,22 @@ def create_linked_list() -> None:
237236
7
238237
8
239238
9
239+
>>> linked_list = LinkedList()
240+
>>> linked_list.insert_at_position(position=1, value=10)
241+
>>> str(linked_list)
242+
'10'
243+
>>> linked_list.insert_at_position(position=2, value=20)
244+
>>> str(linked_list)
245+
'10 20'
246+
>>> linked_list.insert_at_position(position=1, value=30)
247+
>>> str(linked_list)
248+
'30 10 20'
249+
>>> linked_list.insert_at_position(position=3, value=40)
250+
>>> str(linked_list)
251+
'30 10 40 20'
252+
>>> linked_list.insert_at_position(position=5, value=50)
253+
>>> str(linked_list)
254+
'30 10 40 20 50'
240255
"""
241256

242257

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /