# leetcode Remove Duplicates 整理

• 26. Remove Duplicates from Sorted Array
• 80. Remove Duplicates from Sorted Array II
• 82. Remove Duplicates from Sorted List II
• 83. Remove Duplicates from Sorted List

### 26. Remove Duplicates from Sorted Array

Given a sorted array, remove the duplicates in-place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

Example:

Given nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.
It doesn’t matter what you leave beyond the new length.

C++

Python

### 80. Remove Duplicates from Sorted Array II

What if duplicates are allowed at most twice?

For example,
Given sorted array nums = [1,1,1,2,2,3],

Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn’t matter what you leave beyond the new length.

i指向当前元素的第一个位置，j指向下一个元素第一位置。 j-i >＝２说明有两个元素，应在写一次。复杂度为O(n)

C++

Python

C++

Python

### 83. Remove Duplicates from Sorted List

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

For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.

C++

Python

### 82. Remove Duplicates from Sorted List II

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.

For example,
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.

• 当q==p->next说明无重复元素，应该p=q
• 否则，p->next=q->next

C++

python

• 26. Remove Duplicates from Sorted Array
• 80. Remove Duplicates from Sorted Array II
• 82. Remove Duplicates from Sorted List II
• 83. Remove Duplicates from Sorted List