fixes #25173; SinglyLinkedList.remove broken / AssertionDefect (#25175)

fixes #25173

(cherry picked from commit 51a9ada043)
This commit is contained in:
ringabout
2025-09-16 23:05:09 +08:00
committed by narimiran
parent edeac84e47
commit 2fbb793f4e
2 changed files with 14 additions and 0 deletions

View File

@@ -710,6 +710,8 @@ proc remove*[T](L: var SinglyLinkedList[T], n: SinglyLinkedNode[T]): bool {.disc
L.head = n.next
if L.tail.next == n:
L.tail.next = L.head # restore cycle
if L.tail == n:
L.tail = nil # reset tail if we removed the last node
else:
var prev = L.head
while prev.next != n and prev.next != nil:

View File

@@ -273,5 +273,17 @@ template main =
list.add(n4)
doAssert list.toSeq == @["sonic", "the", "hedgehog"]
block:
var list = initSinglyLinkedList[int]()
list.add(4)
list.remove(list.head)
list.add(5)
list.remove(list.head)
list.add(6)
static: main()
main()