title: Python Singly Linked Lists with Tails
body: <p>I have a linked list that I would like to implement as a queue. I have figured out that it works but I have a question about why this specific part works. Here is the code:</p>
  
  <pre><code>class LinkedQueue:
      class _Node:
          def __init__(self, element, next):
              self._element = element
              self._next = next
  
      def __init__(self):
          self._head = None
          self._tail = None
          self._size = 0
  
      def enqueue(self, e):
          newest = self._Node(e, None)
          if self.is_empty():
              self._head = newest
          else:
              self._tail._next = newest
          self._tail = newest
          self._size += 1
  </code></pre>
  
  <p>If the queue is empty and I enqueue, the first conditional in enqueue is true and is triggered and newest is assigned to self._head. But, when I add another element, the else block is triggered, and self._tail._next is assigned newest. This, apparently, changes the _next variable for _head. However, when assigning a third element, the _next variable for _head is not changed. Why is this so? Also, shouldn't _tail be changed by newest, and as such the _next variable is overridden by the newly assigned None?</p>
  
  <p>The code works completely, and I understand the linked list logic, but I don't understand why this specific code works.</p>