diff --git "a/lcof/\351\235\242\350\257\225\351\242\23018. \345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\350\212\202\347\202\271/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23018. \345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\350\212\202\347\202\271/README.md" index 41be8be5e9e37..96ff0975fea8e 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23018. \345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\350\212\202\347\202\271/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23018. \345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\350\212\202\347\202\271/README.md" @@ -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 + } +} +``` + diff --git "a/lcof/\351\235\242\350\257\225\351\242\23018. \345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\350\212\202\347\202\271/Solution.swift" "b/lcof/\351\235\242\350\257\225\351\242\23018. \345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\350\212\202\347\202\271/Solution.swift" new file mode 100644 index 0000000000000..eea97febcc071 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23018. \345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\350\212\202\347\202\271/Solution.swift" @@ -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 + } +} \ No newline at end of file