MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / DelAtEnd

Method DelAtEnd

structure/linkedlist/singlylinkedlist.go:65–85  ·  view source on GitHub ↗

DelAtEnd deletes the snode at the tail(end) of the list and returns its value. Returns false if the list is empty.

()

Source from the content-addressed store, hash-verified

63// DelAtEnd deletes the snode at the tail(end) of the list
64// and returns its value. Returns false if the list is empty.
65func (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

Callers 1

DelByPosMethod · 0.95

Calls 1

DelAtBegMethod · 0.95

Tested by

no test coverage detected