Skip to content

Instantly share code, notes, and snippets.

@Pasanpr Pasanpr/linked_list_repr.py Secret
Created Sep 5, 2018

Embed
What would you like to do?
Implementation for __repr__ method on LinkedList
def __repr__(self):
"""
Return 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 is None:
nodes.append("[Tail: %s]" % current.data)
else:
nodes.append("[%s]" % current.data)
current = current.get_next()
return '-> '.join(nodes)
@sstylianides

This comment has been minimized.

Copy link

commented Sep 20, 2018

line 11 and line 15 is incorrect. should be this:

def __repr__(self):
     """
     Return 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.