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

[Swift]LeetCode82.删除排序链表中的重复元素II|RemoveDuplicatesfromSortedListII ...

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

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

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

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

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

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.

Example 1:

Input: 1->2->3->3->4->4->5
Output: 1->2->5

Example 2:

Input: 1->1->1->2->3
Output: 2->3

给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字。

示例 1:

输入: 1->2->3->3->4->4->5
输出: 1->2->5

示例 2:

输入: 1->1->1->2->3
输出: 2->3

28ms
 1 class Solution {
 2     func deleteDuplicates(_ head: ListNode?) -> ListNode? {
 3         var node = head
 4         var result: ListNode? = nil
 5         var prevNode: ListNode? = nil
 6         while node != nil {
 7             var next = node!.next
 8             if next != nil && next!.val == node!.val {
 9                 while next != nil && next!.val == node!.val {
10                     next = next!.next
11                 }
12                 if prevNode != nil {
13                     prevNode!.next = next
14                 } else {
15                     if result == nil { result = prevNode }
16                 }
17                 node = next
18             } else {
19                 prevNode = node
20                 if result == nil { result = prevNode }
21                 node = next
22             }
23         }
24         return result
25     }
26 }

32ms

 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 deleteDuplicates(_ head: ListNode?) -> ListNode? {
14     let dummyHead = ListNode(0)
15     dummyHead.next = head
16     var previous: ListNode? = dummyHead
17     var current = previous
18     
19     while current != nil {
20       while current?.next != nil && previous?.next?.val == current?.next?.val {
21         current = current?.next
22       }
23       
24       if previous?.next === current {
25         previous = previous?.next
26       } else {
27         previous?.next = current?.next
28       }
29       current = current?.next
30     }
31     
32     return dummyHead.next
33   }
34 }

44ms

 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 deleteDuplicates(_ head: ListNode?) -> ListNode? {
14         let root = ListNode(-1)
15         root.next = head
16         var node: ListNode? = root
17         while node?.next != nil {
18             if node?.next?.val == node?.next?.next?.val {
19                 let val = node?.next?.val
20                 while node?.next != nil && node?.next?.val == val {
21                     node?.next = node?.next?.next
22                 }
23             } else {
24                 node = node?.next
25             }
26         }
27         return root.next
28     }
29 }

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 deleteDuplicates(_ head: ListNode?) -> ListNode? {
14         if head == nil {
15             return head
16         }
17         let dummy:ListNode? = ListNode(0)
18         dummy?.next = head
19         var pre = dummy
20         while pre?.next != nil {
21             var same = false
22             var node = pre?.next
23             while node?.next != nil && node!.val == node?.next!.val {
24                 node = node?.next
25                 same = true
26             }
27             if same {
28                 pre?.next = node?.next
29             } else {
30                 pre = pre?.next
31             }
32         }
33         return dummy?.next
34     }
35 }

 


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
swift:打造你自己的折线图发布时间:2022-07-13
下一篇:
5.15 使用is语句检查实例的类型 [Swift原创教程]发布时间: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