The following is the solution in this book,
need “current = prev?.next“
and then “continue”
var prev = head
var current = head?.next
while let currentNode = current {
guard currentNode.value != value else {
prev?.next = currentNode.next
👉current = prev?.next
👉continue
}
// more to come
}
But I found that they can be replaced with other code, as follows:
👉current = prev
// here is not needed “continue”
It seems to work. I wonder if this is feasible?
The complete code is like this:
func removeAll<T: Equatable>(_ value: T, in list: LinkedList<T>) -> LinkedList<T> {
var head = list.head
var tail = list.tail
while head != nil && head?.value == value {
head = head?.next
}
var prev = head
var current = head?.next
while current != nil {
if current?.next == nil {
tail = current
}
if current?.value == value {
prev?.next = current?.next
current = prev
}
prev = current
current = current?.next
}
tail = prev
var list = LinkedList<T>()
list.head = head
list.tail = tail
return list
}