Skip to content

Instantly share code, notes, and snippets.

@NellMartinez
Last active December 29, 2021 23:29
Show Gist options
  • Save NellMartinez/eabed53b9fc4db1b912ea61de31b1a50 to your computer and use it in GitHub Desktop.
Save NellMartinez/eabed53b9fc4db1b912ea61de31b1a50 to your computer and use it in GitHub Desktop.
Append to a single linked list in constant time
def create_linked_list_better(input_list):
head = None
tail = None
for value in input_list:
if head is None:
head = Node(value)
tail = head # when we only have 1 node, head and tail refer to the same node
else:
tail.next = Node(value) # attach the new node to the `next` of tail
tail = tail.next # update the tail
return head
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment