删除有序链表中重复出现的元素
#include<iostream>
using namespace std;
struct ListNode {
int val;
struct ListNode *next;
ListNode(int val_) :val(val_) {}
};
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if (head == nullptr)
return nullptr;
ListNode* newHead = new ListNode(-1);
newHead->next = head;
while (head != nullptr && head->next != nullptr)
{
while (head->next != nullptr && head->val == head->next->val)
{
head->next = head->next->next;
}
head = head->next;
}
return newHead->next;
}
};