DelAtEnd deletes the snode at the tail(end) of the list and returns its value. Returns false if the list is empty.
()
| 63 | // DelAtEnd deletes the snode at the tail(end) of the list |
| 64 | // and returns its value. Returns false if the list is empty. |
| 65 | func (ll *Singly[T]) DelAtEnd() (T, bool) { |
| 66 | if ll.Head == nil { |
| 67 | var r T |
| 68 | return r, false |
| 69 | } |
| 70 | |
| 71 | if ll.Head.Next == nil { |
| 72 | return ll.DelAtBeg() |
| 73 | } |
| 74 | |
| 75 | cur := ll.Head |
| 76 | |
| 77 | for ; cur.Next.Next != nil; cur = cur.Next { |
| 78 | } |
| 79 | |
| 80 | retval := cur.Next.Val |
| 81 | cur.Next = nil |
| 82 | ll.length-- |
| 83 | return retval, true |
| 84 | |
| 85 | } |
| 86 | |
| 87 | // DelByPos deletes the node at the middle based on position in the list |
| 88 | // and returns its value. Returns false if the list is empty or length is not more than given position |