[Swift]LeetCode82. 删除排序链表中的重复元素 II | Remove Duplicates from Sorted List II
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9935318.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
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 }