【7. Linked List Challenges】Solution to Challenge 5

The following is the solution in this book,
need :point_right:“current = prev?.next“
and then :point_right:“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
}

@jomoka @kelvin_lau any comments?