diff --git "a/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\345\246\202\344\275\225\345\216\273\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240.md" "b/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\345\246\202\344\275\225\345\216\273\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240.md" index 678236faf657edff2ae4dc4d413b24f000b64634..3e1cd14721f47095691542ee38f7f1bc23cb7ae6 100644 --- "a/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\345\246\202\344\275\225\345\216\273\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240.md" +++ "b/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\345\246\202\344\275\225\345\216\273\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240.md" @@ -66,6 +66,48 @@ ListNode deleteDuplicates(ListNode head) { ![labuladong](../pictures/labuladong.png) +[eric wang](https://www.github.com/eric496) 提供有序数组 Python3 代码 + +```python +def removeDuplicates(self, nums: List[int]) -> int: + n = len(nums) + + if n == 0: + return 0 + + slow, fast = 0, 1 + + while fast < n: + if nums[fast] != nums[slow]: + slow += 1 + nums[slow] = nums[fast] + + fast += 1 + + return slow + 1 +``` + +[eric wang](https://www.github.com/eric496) 提供有序链表 Python3 代码 + +```python +def deleteDuplicates(self, head: ListNode) -> ListNode: + if not head: + return head + + slow, fast = head, head.next + + while fast: + if fast.val != slow.val: + slow.next = fast + slow = slow.next + + fast = fast.next + + # 断开与后面重复元素的连接 + slow.next = None + return head +``` + [上一篇:如何高效解决接雨水问题](../高频面试系列/接雨水.md) [下一篇:如何寻找最长回文子串](../高频面试系列/最长回文子串.md)