Hey,
I was solving Linked Lists Challenge 3 Linked List Challenges and I pretty much managed to write the exact solution minus one line. Without that one line the solution gets stuck, but I can’t figure out why. The accompanying text does not talk of its exact significance either.
mutating func reverse() {
tail = head
var prev = head
var current = head?.next
prev?.next = nil
while current != nil {
let next = current?.next
current?.next = prev
prev = current
current = next
}
head = prev
}
I hadn’t included the following line
prev?.next = nil
I know its eventually important, because the new tail shouldn’t have a next, but what I couldn’t understand was that why does the algorithm not run unless I add this line.