Your `insert` was too complicated for me to understand so I rewrote the code to: proc push*[T](list: var LinkedList[T], val: T) = ## Appends a node with the given `val` to `list`. #list.insert list.last, nil, val var node = ListNode[T](val: val) node.prev = list.last # node.next remains nil list.last = node if list.first.isNil: list.first = node proc unshift*[T](list: var LinkedList[T], val: T) = ## Prepends a node with the given `val` to `list`. #list.insert nil, list.first, val var node = ListNode[T](val: val) node.next = list.first # node.prev remains nil list.first = node if list.last.isNil: list.last = node Run
And the errors that valgrind showed disappeared.