# leetcode-18-remove

283. Move Zeroes

27. Remove Element

i从前向后，找到第一个是val的位置，nums[i]和nums[j]交换。最后，nums[j]后面都是val，所以返回j+1即是想要的长度。需要注意

```int removeElement(vector<int>& nums, int val) {
if (nums.size() == 0)
return 0;
if (nums.size() == 1) {
if (nums[0] == val)
return 0;
else
return 1;
}
int i,j;
j = nums.size() - 1;
while (nums[j] == val) {
j --;
if (j < 0)
return 0;
}
for (i = 0; i <= j; i++) {
if (nums[i] == val) {
nums[i] = nums[j];
nums[j] = val;
j --;
// nums[j] may be val. then move forward.
while (nums[j] == val)
j--;
continue;
}

}
return j+1;
}
```

26. Remove Duplicates from Sorted Array

```int removeDuplicates(vector<int>& nums) {
if (nums.size() < 2)
return nums.size();
int i = 0;
int j = i + 1;
while(i < nums.size()-1) {
if (nums[j] == nums[i]) {
while (nums[j] == nums[i]) {
j++;
if (j == nums.size())
return i+1;
}
nums[i+1] = nums[j];
i++;
continue;
}
else {
i++;
j++;
}
}
return i+1;
}　　```

```ListNode* removeElements(ListNode* head, int val) {
// if val is at head
}
while (second != NULL) {
if (second->val == val) {
// judge if second is NULL. serial val
while(second != NULL && second->val == val) {
second = second->next;
}
if (second == NULL) {
first->next = NULL;
}
else {
first->next = second;
first = second;
if (first != NULL)
second = first->next;
else
second = NULL;
}
continue;
}
// not val
first = second;
second = second->next;
}
}```

## LeetCode：Remove Nth Node From End of List

1.题目名称 Remove Nth Node From End of List(移除链表中倒数第n项) 2.题目地址 https://leetcode.com/problems/remove-nth-node-from-end-of-list/ 3.题目内容 英文:Given a linked list, remove the nth node from the end of list and return its head. 中文:给出一个链表,删去链表中的倒数第n项,返回链表首节点 例如:

## 【LeetCode】Remove Duplicates from Sorted Array 解题报告

[LeetCode]Remove Duplicates from Sorted Array 解题报告 标签(空格分隔): LeetCode [LeetCode] https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Total Accepted: 129010 Total Submissions: 384622 Difficulty: Easy Question Given a sorted array, remov

## [LeetCode 题解]: 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. 题意:给定一个已排序的链表,删除其中重复的元素,保证每个元素有且仅有一个. 使用两个指针,分别指向前一个元素,和当前元素,

## LeetCode：Remove Element

Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length. Solution: class Solution { public: int removeElement(int

## LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] &lt;c++&gt;

LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数大于两次,删除多余的复制,返回删除后数组长度,要求不另开内存空间. C++ 献上自己丑陋无比的代码.相当于自己实现一个带计数器的unique函数 class Solution { public: int removeDuplicates(std::vector<int>& nums) {

## [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)

https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C%2B%2B-Java-Python-Ruby 描述 Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1