I solved the same exercise some weeks ago, without using `.cursor` pragma 
though.

After adding the pragma, my code kept working flawlessly. Even after changing 
the approach to use a similar `insert` proc as provided in the question.

After some tinkering around I noticed that I used the object constructor to its 
full power and already set `prev` and `next` fields here, which even allows to 
shorten the `insert` proc a little bit
    
    
    proc insert[T](list: var LinkedList[T]; prev, next: ListNode[T]; val: T) =
      let node = ListNode[T](val: val, prev: prev, next: next)
      
      if prev.isNil:
        list.first = node
      else:
        #node.prev = prev  # (redundant)
        prev.next = node
      
      if next.isNil:
        list.last = node
      else:
        #node.next = next  # (redundant)
        next.prev = node
    
    
    Run

This now seems to work perfectly fine with the rest of the given code snippet.

However I'm not able to explain why the initial solution didn't work. Maybe 
someone else can, based on my observations.

Reply via email to