Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions lcof/面试题18. 删除链表的节点/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,39 @@ public class Solution {
}
```

#### Swift

```swift
/**
* Definition for singly-linked list.
* public class ListNode {
* var val: Int
* var next: ListNode?
* init(_ val: Int, _ next: ListNode? = nil) {
* self.val = val
* self.next = next
* }
* }
*/

class Solution {
func deleteNode(_ head: ListNode?, _ val: Int) -> ListNode? {
let dummy = ListNode(0, head)
var current: ListNode? = dummy

while current?.next != nil {
if current?.next?.val == val {
current?.next = current?.next?.next
break
}
current = current?.next
}

return dummy.next
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
28 changes: 28 additions & 0 deletions lcof/面试题18. 删除链表的节点/Solution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Definition for singly-linked list.
* public class ListNode {
* var val: Int
* var next: ListNode?
* init(_ val: Int, _ next: ListNode? = nil) {
* self.val = val
* self.next = next
* }
* }
*/

class Solution {
func deleteNode(_ head: ListNode?, _ val: Int) -> ListNode? {
let dummy = ListNode(0, head)
var current: ListNode? = dummy

while current?.next != nil {
if current?.next?.val == val {
current?.next = current?.next?.next
break
}
current = current?.next
}

return dummy.next
}
}