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

fixes #25173
This commit is contained in:
ringabout
2025-09-16 23:05:09 +08:00
committed by GitHub
parent 40fe59b6ef
commit 51a9ada043
2 changed files with 14 additions and 0 deletions

View File

@@ -712,6 +712,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 {.cursor.} = 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()