class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
ListNode* headOfHead = new ListNode(0); //构建头结点
headOfHead -> next = head;
ListNode* priorPtr = headOfHead;
for (ListNode* ptr =head; ptr != NULL; ){
ListNode* startPtr = ptr;
int StartNum = ptr->val;
ptr = ptr->next;
while (ptr != NULL && ptr->val == StartNum)//统计删除范围
ptr = ptr ->next;
if (startPtr->next != ptr)//判断是否删除了元素
priorPtr->next = ptr;
else
priorPtr = startPtr;
}
return headOfHead->next;
}
Remove Duplicates from Sorted List II
最新推荐文章于 2020-09-18 20:58:16 发布