struct SinglyLinkList {
var head: SinglyLinkListNode?
var tail: SinglyLinkListNode? {
guard
head != nil
else { return nil }
var currentNode = head
while currentNode?.next != nil {
currentNode = currentNode?.next
}
return currentNode
}
mutating func removeWhoseTotalIsZero() {
var current = self.head
while current != nil {
var fast = current?.next
var nodeDeleted = false
while fast != nil {
if let fastValue = fast?.value, let currentValue = current?.value,
(fastValue + currentValue) == 0 {
nodeDeleted = true
current = current?.next
fast = fast?.next
delete(fastValue)
delete(currentValue)
} else {
fast = fast?.next
}
}
if !nodeDeleted { current = current?.next }
}
}
}