Skip to content

Instantly share code, notes, and snippets.

@fchikwekwe
Last active May 5, 2019 20:12
Show Gist options
  • Save fchikwekwe/d6022f48d23cd06ea063031f1d5efd11 to your computer and use it in GitHub Desktop.
Save fchikwekwe/d6022f48d23cd06ea063031f1d5efd11 to your computer and use it in GitHub Desktop.
def length(self):
"""Return the length of this linked list by traversing its nodes."""
node_count = 0 # initial count is zero
current = self.head # start at the head
# Loop through all nodes
while current is not None:
current = current.next
# add one to the counter each time
node_count += 1
# return the total length
return node_count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment