Hi guys, I am struggling with exercise 13 in the book “Learn More Python the Hard Way”. The issue lies in finding the logic by which the self.begin attribute in the controlling class keeps getting new nodes attached to it (resulting into begin.next.next.next etc. as the list grows). So the first if condition (in the script below) makes sense to me, but I am not seeing the logic in the else statement that could potentially bring the nodes from the end attribute to the begin attribute – as the else statement only targets the self.end attribute.
Below is the fragment from Zed Shaw’s github/video and I’ve also uploaded a flow chart on how I see the logic not making sense to me in extending the list. Basically used colors to differentiate between the nodes in the if, elif and finally else statement
Hope someone sees where I go wrong in the though proces.
class SingleLinkedList(object):
def __init__(self):
self.begin = None
self.end = None
def push(self, obj):
"""Appends a new value on the end of the list."""
node = SingleLinkedListNode(obj, None)
if self.begin == None:
# nothing net
self.begin = node
self.end = self.begin
elif self.begin == self.end:
self.begin.next = node
self.end = node
else:
self.end.next = node
self.end = node
assert self.begin != self.end
assert self.end.next == None