I'm trying to implement LinkedList operation in Golang from scratch. But I found a problem when dealing with removing first element. My approach is using OOP style, but it seems the first element is not removed. This is the code I write,
type LinkedList struct {
Value int
next *LinkedList
}
func (ll *LinkedList) Remove(index int) error {
pointer := ll
var pointerPrev *LinkedList = nil
current := 0
for current < index {
pointerPrev = pointer
pointer = pointer.next
current++
}
if pointer == ll {
ll = ll.next // this line is problematic
pointer = nil
} else {
if pointer.next == nil {
pointerPrev.next = nil
} else {
pointerPrev.next = pointer.next
pointer = nil
}
}
return nil
}
Any suggestion how I implement this way of removing without returning new LinkedList pointer?