Skip to content

Instantly share code, notes, and snippets.

@wanderindev
Created June 19, 2022 02:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wanderindev/b6dbc2824f1ae5bbe42bb003983927d8 to your computer and use it in GitHub Desktop.
Save wanderindev/b6dbc2824f1ae5bbe42bb003983927d8 to your computer and use it in GitHub Desktop.
def get_last_node(self) -> "Node":
"""Return the last node in the linked list"""
current_node = self.head
while current_node.next_node is not None:
current_node = current_node.next_node
return current_node
def get_node(self, node_data: Any) -> Union["Node", None]:
"""Return the first node whose data is node_data or None"""
current_node = self.head
while current_node.data != node_data:
if not current_node:
return None
current_node = current_node.next_node
return current_node
def get_prev_node(self, node_data: Any) -> Union["Node", None]:
"""Return the node before first node whose data is node_data or None"""
current_node = self.head
prev_node = None
while current_node.data != node_data:
if not current_node:
return None
prev_node = current_node
current_node = current_node.next_node
return prev_node
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment