• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode203.移除链表元素|RemoveLinkedListElements

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10184259.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

Remove all elements from a linked list of integers that have value val.

Example:

Input:  1->2->6->3->4->5->6, val = 6
Output: 1->2->3->4->5

删除链表中等于给定值 val 的所有节点。

示例:

输入: 1->2->6->3->4->5->6, val = 6
输出: 1->2->3->4->5

72ms
 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     public var val: Int
 5  *     public var next: ListNode?
 6  *     public init(_ val: Int) {
 7  *         self.val = val
 8  *         self.next = nil
 9  *     }
10  * }
11  */
12 class Solution {
13     func removeElements(_ head: ListNode?, _ val: Int) -> ListNode? {
14         guard head != nil else { return nil }
15         
16         var head = head
17         while head != nil, head!.val == val {
18             head = head?.next
19         }
20         
21         var prev = head
22         var current = head?.next
23         
24         while let curr = current {
25             if curr.val == val {
26                 prev?.next = curr.next
27                 current = curr.next
28                 continue
29             }
30             prev = curr
31             current = curr.next
32         }
33         
34         return head
35     }
36 }

76ms

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     public var val: Int
 5  *     public var next: ListNode?
 6  *     public init(_ val: Int) {
 7  *         self.val = val
 8  *         self.next = nil
 9  *     }
10  * }
11  */
12 class Solution {
13     func removeElements(_ head: ListNode?, _ val: Int) -> ListNode? {
14         guard let head = head else { return nil }
15         head.next = removeElements(head.next, val)
16         return head.val == val ? head.next : head
17     }
18 }

80ms

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     public var val: Int
 5  *     public var next: ListNode?
 6  *     public init(_ val: Int) {
 7  *         self.val = val
 8  *         self.next = nil
 9  *     }
10  * }
11  */
12 class Solution {
13     func removeElements(_ head: ListNode?, _ val: Int) -> ListNode? {
14 
15         var dummy = ListNode(-1)
16         dummy.next = head
17         var cur = dummy
18         while cur.next != nil {
19             if cur.next!.val == val {
20                 cur.next = cur.next!.next
21             }
22             else{
23                 cur = cur.next!
24             }
25         }
26         return dummy.next
27     }
28 }

96ms

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     public var val: Int
 5  *     public var next: ListNode?
 6  *     public init(_ val: Int) {
 7  *         self.val = val
 8  *         self.next = nil
 9  *     }
10  * }
11  */
12 
13 class Solution {
14     func removeElements(_ head: ListNode?, _ val: Int) -> ListNode? {
15         guard let _ = head else {
16             return nil
17         }
18         
19         //在表头添加一个哨兵节点
20         let fakeNode = ListNode(NSNotFound)
21         fakeNode.next = head
22         var prev:ListNode = fakeNode
23         var current:ListNode? = head
24         while let tmp = current {
25             if tmp.val != val {
26                 prev.next = tmp
27                 prev = prev.next!
28             }
29             current = tmp.next
30         }
31         
32         if prev.next != nil {
33             prev.next = nil
34         }
35         return fakeNode.next
36     }
37 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift编程复习补充笔记发布时间:2022-07-13
下一篇:
[Swift]LeetCode29.两数相除|DivideTwoIntegers发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap