Swift 3 delete nodes from singly linked list -


i trying remove elements linked list of integers have value val. necessary set removed nodes nil free memory?

func removeelements(_ head: node?, _ val: int) -> node? {     var first = head     var current = head     var prev: node?     while current != nil {         if current?.val != val {             prev = current         } else if current?.val == first?.val {             var oldfirst = first             first = current?.next             oldfirst = nil // line necessary?         } else {             prev?.next = current?.next // need set current nil?         }         current = current?.next     }     return first } 

oldfirst = nil sets variable in current scope nil.
again, current variable in local scope, gets dereferenced , cleaned once leave scope.

if have no strong references object anymore released because swift uses automatic reference counting (arc: https://en.wikipedia.org/wiki/automatic_reference_counting)

i not sure why have 2nd case in code. guess checks case current node has value val compare first.val instead of val


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -