描述
删除给出链表中的重复元素(链表中元素从小到大有序),使链表中的所有元素都只出现一次
例如:
给出的链表为1→1→2,返回1→2.
解题思路
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
ListNode* p=head;
if(head==nullptr) return head;
while(p->next!=nullptr){
if(p->val==p->next->val){
p->next=p->next->next;
}else p=p->next;
}
return head;
}
};