题目链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/
题目如下:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
//tip:
//只有头节点可能发生改变,才需要增加一个dummyhead,否则,不需要,正常pre和cur指针即可
if(head==NULL) return head;
ListNode* pre=head;
ListNode* cur=pre->next;
while(cur!=NULL){
if(cur->val==pre->val){
pre->next=cur->next;
}
else {
pre=pre->next;
}
cur=cur->next;
}
return head;
}
};