提交 4a504699 编写于 作者: E eric496 提交者: labuladong

添加有序数组去重Python3代码

上级 2ec69eb2
......@@ -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)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册