题解 | #删除有序链表中重复的元素-II#
删除有序链表中重复的元素-II
http://www.nowcoder.com/practice/71cef9f8b5564579bf7ed93fbe0b2024
题目
题解:双指针
class Solution { public: /** * * @param head ListNode类 * @return ListNode类 */ ListNode* deleteDuplicates(ListNode* head) { // write code here ListNode *res=new ListNode(0),*cur=head,*pre=res; res->next=head; while(cur && cur->next) { bool flag=false; while(cur->next && cur->val==cur->next->val)//有相等节点 { flag=true; cur=cur->next; } if(flag)pre->next=cur->next;//跳过相同节点 else pre=cur;//不相等节点 cur=cur->next; } return res->next; } };