为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode83. 删除排序链表中的重复元素 | Remove Duplicates from Sorted List

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

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

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

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

Given a sorted linked list, delete all duplicates such that each element appear only once.

Example 1:

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

Example 2:

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

给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。

示例 1:

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

示例 2:

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

28ms
 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         var temp = head
15         while temp != nil {
16             if temp?.next?.val == temp?.val {
17                 temp?.next = temp?.next?.next
18             } else {
19                temp = temp?.next 
20             }
21         }
22         
23         return head
24     }
25 }

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         
15         var node: ListNode? = head
16         var next: ListNode? = nil
17         
18         while node != nil {
19             
20             next = node!.next
21             while next != nil && next!.val == node!.val {
22                 next = next!.next
23             }
24             
25             node!.next = next
26             node = next
27         }
28         
29         return head
30     }
31 }

60ms

 1 class Solution {
 2     func deleteDuplicates(_ head: ListNode?) -> ListNode? {
 3         var r = head
 4         while r?.next != nil {
 5             if r?.val == r?.next?.val {
 6                 r?.next = r?.next?.next
 7             } else {
 8                 r = r?.next
 9             }
10         }
11         return head
12     }
13 }

64ms

 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     
14     func deleteDuplicates(_ head: ListNode?) -> ListNode? {
15         guard var head = head else{
16             return nil
17         }
18         
19         func remove(_ current:ListNode, _ previous:ListNode? = nil) {
20             guard let pre = previous else{
21                 if current.next != nil {
22                     remove(current.next!,current)
23                 }
24                 return
25             }
26             if pre.val == current.val {
27                 pre.next = current.next
28                 if current.next != nil {
29                     remove(current.next!,pre)
30                 }
31                 return
32             }else{
33                 if current.next != nil {
34                     remove(current.next!,current)
35                 }
36             }
37         }
38         
39         remove(head)
40         return head
41     }
42     
43 }

 

posted @ 2018-09-25 09:34  为敢技术  阅读(332)  评论(2编辑  收藏  举报