Last active
December 29, 2021 23:29
-
-
Save NellMartinez/eabed53b9fc4db1b912ea61de31b1a50 to your computer and use it in GitHub Desktop.
Append to a single linked list in constant time
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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