C++,双指针,用智能指针释放掉伪头
删除有序链表中重复的元素-II
http://www.nowcoder.com/practice/71cef9f8b5564579bf7ed93fbe0b2024
不怎么写代码,所以也不知道用的对不对,反正提交通过了,就当是对了吧。 如果用的不对或者有建议,麻烦请留言指导,我也想学会更好地使用智能指针,谢谢啦~
if (!head) {
return head;
}
unique_ptr<ListNode> dummy(new ListNode(0));
dummy->next=head;
ListNode* cur = dummy.get();
while (cur->next && cur->next->next) {
if (cur->next->val == cur->next->next->val) {
int x = cur->next->val;
while (cur->next && cur->next->val == x) {
cur->next = cur->next->next;
}
}
else {
cur = cur->next;
}
}
return dummy->next;