Hello all,
I am trying to solve the exercise:
‘WARNING! Take the time now to figure out how to manually build a list using just the
SingleLinkedListNode class and then manually walk through it. This is a good
45-minute hack spike to try at this point in the exercise.’ (LMPTHW, pg. 51)
Problem:
I do not understand:
- what is exactly meant by ‘manually’ in the exercise description above.
- the code below and how to create instances of the class that represent ‘nodes’ in a ‘Single Linked List’.
1 class SingleLinkedListNode(object):
2
3 def __init__(self, value, nxt):
4 self.value = value
5 self.next = nxt
6
7 def __repr__(self):
8 nval = self.next and self.next.value or None
9 return f"[{self.value}:{repr(nval)}]"
My code is:
head = SingleLinkedListNode(None, 1)
"""the head of the SLL has no value, but contains the address of the next node, given that 1 represents the address of the next node"""
node1 = SingleLinkedListNode(10, 2) # self.value = 10, self.next =2
My code gives an error (es expected)
ERROR:
nval = self.next and self.next.value or None
AttributeError: ‘int’ object has no attribute ‘value’
I understand the error:
for variable ‘head’ self.next = 2 (integer), but, the program expects another object of class SingleLinkedListNode. But somehow this would mean that the 2nd parameter of head (the object) is already supposed to be created (but I do not see how I can create it before creating head).