def insert(self, node, before=None):
        """Insert a new node in the list.

        If *before* is specified, the new node is inserted before this node.
        Otherwise, the node is inserted at the end of the list.
        """
        node._list = self
        if self._first is None:
            self._first = self._last = node  # first node in list
            self._size += 1
            return node
        if before is None:
            self._last._next = node  # insert as last node
            node._prev = self._last
            self._last = node
        else:
            node._next = before
            node._prev = before._prev
            if node._prev:
                node._prev._next = node
            else:
                self._first = node  # inserting as first node
            node._next._prev = node
        self._size += 1
        return node
Insert a new node in the list.

        If *before* is specified, the new node is inserted before this node.
        Otherwise, the node is inserted at the end of the list.