题解 | #删除有序链表中重复的元素-II#
删除有序链表中重复的元素-II
http://www.nowcoder.com/practice/71cef9f8b5564579bf7ed93fbe0b2024
- 注意创建一个node
- 注意cur->next = cur->next->next的妙用,使得cur->next有不同的意义。
- 注意pre和head都可以维护这个新链表
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
class Solution {
public:
/**
*
* @param head ListNode类
* @return ListNode类
*/
ListNode* deleteDuplicates(ListNode* head) {
// write code here
ListNode* newhead = new ListNode(0);//设置一个初始链表
newhead->next = head;//将head链表添加到newhead中
ListNode* pre = newhead;
ListNode* cur = head;
int count = 0;//设置重复次数
while(cur != NULL && cur->next != NULL){//判断条件
if(cur->val == cur->next->val){//如果按照顺序,值相等
cur->next = cur->next->next;//删除该元素
count++;//将count次数加一再次进行判断
}
else{
if(count > 0){
pre->next = cur->next;//将该元素值全部删除
count = 0;
}
else
pre = cur;
cur = cur->next;//递归条件
}
}
if(count > 0)//存在比如{1,2,2},因为删除,所以上述循环条件不进行判断,在此额外进行判断
pre->next = cur->next;
return newhead->next;
}
};算法解析 文章被收录于专栏
这里主要是算法岗的自我思路总结


