★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-ytsbdfos-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.node
Example 1:git
Input: 1->2->3->3->4->4->5 Output: 1->2->5
Example 2:github
Input: 1->1->1->2->3 Output: 2->3
給定一個排序鏈表,刪除全部含有重複數字的節點,只保留原始鏈表中 沒有重複出現 的數字。微信
示例 1:spa
輸入: 1->2->3->3->4->4->5 輸出: 1->2->5
示例 2:code
輸入: 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 }
32mshtm
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 }
44msblog
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 }