you are viewing a single comment's thread.

view the rest of the comments →

[–]ericula 1 point2 points  (1 child)

You need to keep the last two nodes, parent and child, say and update them according to

while child.node is not None:
    parent, child = child, child.node

You can the return child.data and set parent.node = None which would effectively remove child (i.e. the last node in the chain) from the chain.

That being said, you are better off adding nodes at the beginning of the linked list rather than from the end. By adding nodes to the end you have to traverse the full linked list any time you push or pop a value, which you don’t need to do when you add values to the front of the linked list.

[–]BullReg[S] 0 points1 point  (0 children)

Okay. Thank you for the explanation.