这个题目是删除重复节点的重复部分,保留第一个出现的,可以使用双指针跳过重复点。
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) : val(x), next(nullptr) {}
* };
*/
#include <vector>
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @return ListNode类
*/
ListNode* deleteDuplicates(ListNode* head) {
// write code here
if (head == nullptr || head->next == nullptr) {
return head;
}
ListNode* vHead = new ListNode(0);
vHead->next=head;
ListNode* m = vHead;
vector<int> l;
while (head!= nullptr) {
auto it = std::find(l.begin(), l.end(), head->val);
if (it == l.end()) {
l.push_back(head->val);
m->next=head;
m = head;
head = head->next;
}else {
if (head->next!=nullptr) {
// m->next = head->next;
head = head->next;
// m =head;
}else {
m->next = nullptr;
return vHead->next;
}
}
}
return vHead->next;
}
};