题解 | #删除有序链表中重复的元素-I#
删除有序链表中重复的元素-I
http://www.nowcoder.com/practice/c087914fae584da886a0091e877f2c79
双指针,flag标记唯一数,cur标记当前位
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(!head) return head;
// write code here
ListNode* cur = head;
ListNode* flag = head;
while(cur){
if(flag == cur){//相等时,找下一个
cur = cur->next;
}else if(flag != cur && flag->val != cur->val){//不相等时,移动
flag = cur;
}else if(flag != cur && flag->val == cur->val){//删除节点
flag->next = cur->next;
free(cur);
cur = flag->next;
}
}
return head;
}
};