File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -304,6 +304,34 @@ var removeElements = function(head, val) {
304
304
};
305
305
```
306
306
307
+ Swift:
308
+
309
+ ``` swift
310
+ /**
311
+ * Definition for singly-linked list.
312
+ * public class ListNode {
313
+ * public var val: Int
314
+ * public var next: ListNode?
315
+ * public init() { self.val = 0; self.next = nil; }
316
+ * public init(_ val: Int) { self.val = val; self.next = nil; }
317
+ * public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
318
+ * }
319
+ */
320
+ func removeElements (_ head : ListNode? , _ val : Int ) -> ListNode? {
321
+ let dummyNode = ListNode ()
322
+ dummyNode.next = head
323
+ var currentNode = dummyNode
324
+ while let curNext = currentNode.next {
325
+ if curNext.val == val {
326
+ currentNode.next = curNext.next
327
+ } else {
328
+ currentNode = curNext
329
+ }
330
+ }
331
+ return dummyNode.next
332
+ }
333
+ ```
334
+
307
335
308
336
309
337
You can’t perform that action at this time.
0 commit comments