Skip to content

Fix bug for data_structures/linked_list/doubly_linked_list_two.py #12651

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

Merged
Changes from all commits
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
27 changes: 21 additions & 6 deletions data_structures/linked_list/doubly_linked_list_two.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ def set_head(self, node: Node) -> None:
self.insert_before_node(self.head, node)

def set_tail(self, node: Node) -> None:
if self.head is None:
self.set_head(node)
if self.tail is None:
self.head = node
self.tail = node
else:
self.insert_after_node(self.tail, node)

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

node.previous = node_to_insert

def insert_after_node(self, node: Node | None, node_to_insert: Node) -> None:
assert node is not None

def insert_after_node(self, node: Node, node_to_insert: Node) -> None:
node_to_insert.previous = node
node_to_insert.next = node.next

Expand All @@ -127,7 +126,7 @@ def insert_at_position(self, position: int, value: int) -> None:
return
current_position += 1
node = node.next
self.insert_after_node(self.tail, new_node)
self.set_tail(new_node)

def get_node(self, item: int) -> Node:
node = self.head
Expand Down Expand Up @@ -237,6 +236,22 @@ def create_linked_list() -> None:
7
8
9
>>> linked_list = LinkedList()
>>> linked_list.insert_at_position(position=1, value=10)
>>> str(linked_list)
'10'
>>> linked_list.insert_at_position(position=2, value=20)
>>> str(linked_list)
'10 20'
>>> linked_list.insert_at_position(position=1, value=30)
>>> str(linked_list)
'30 10 20'
>>> linked_list.insert_at_position(position=3, value=40)
>>> str(linked_list)
'30 10 40 20'
>>> linked_list.insert_at_position(position=5, value=50)
>>> str(linked_list)
'30 10 40 20 50'
"""


Expand Down