mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
添加0203.移除链表元素 Swift版本
This commit is contained in:
@ -304,6 +304,34 @@ var removeElements = function(head, val) {
|
||||
};
|
||||
```
|
||||
|
||||
Swift:
|
||||
|
||||
```swift
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* public var val: Int
|
||||
* public var next: ListNode?
|
||||
* public init() { self.val = 0; self.next = nil; }
|
||||
* public init(_ val: Int) { self.val = val; self.next = nil; }
|
||||
* public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
|
||||
* }
|
||||
*/
|
||||
func removeElements(_ head: ListNode?, _ val: Int) -> ListNode? {
|
||||
let dummyNode = ListNode()
|
||||
dummyNode.next = head
|
||||
var currentNode = dummyNode
|
||||
while let curNext = currentNode.next {
|
||||
if curNext.val == val {
|
||||
currentNode.next = curNext.next
|
||||
} else {
|
||||
currentNode = curNext
|
||||
}
|
||||
}
|
||||
return dummyNode.next
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user