Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Introduction to Data Structures!
You have completed Introduction to Data Structures!
Preview
In the previous video we created the outlines of a linked list but without the ability to add nodes it's not really a list. Let's define an add operation to prepend nodes
Code Snippet for repr Function:
def __repr__(self):
"""
Returns a string representation of the list.
Takes O(n) time
"""
nodes = []
current = self.head
while current:
if current is self.head:
nodes.append("[Head: %s]" % current.data)
elif current.next_node is None:
nodes.append("[Tail: %s]" % current.data)
else:
nodes.append("[%s]" % current.data)
current = current.next_node
return '-> '.join(nodes)
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Technically speaking, there are three
ways we can add data to a list.
0:00
We can add nodes at the head of the list,
0:00
which means that the most recent
node we created will be the head.
0:03
And the first node we created will be
the tail or we could flip that around.
0:07
Most recent nodes
are the tail of the list and
0:11
the first node to be added is the head.
0:13
I mentioned that one of the advantages
of linked lists over arrays
0:16
is that inserting data into the list is
much more efficient than to the array.
0:19
This is only true if we're
inserting at the head or the tail.
0:23
Technically speaking,
this isn't an insert, and
0:27
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up