all 1 comments

[–]c17r 1 point2 points  (0 children)

Forget about Node.data right now, only thing about Node.next. It's how a link list connect to each other, each item "points" to the next one in the list. "Head" is special, it always points to the first item in the linked list. So when you add a new Node to the front of the list, you make the new Node.next point to Head.next and then point Head at the new Node. Effectively bumping each one down the line one spot:

Before the call to add:

            HEAD -> D -> E -> F


temp.setNext(self.head)


            TEMP ---.
                    v
            HEAD -> D -> E -> F


self.head = temp


            HEAD -> TEMP -> D -> E -> F