# (LeetCode 82)Remove Duplicates from Sorted List II

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.

For example,
Given `1->2->3->3->4->4->5`, return `1->2->5`.
Given `1->1->1->2->3`, return `2->3`.

1、递归思路：

2、非递归思路：

## 参考代码：

```/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:

}
else{
return deleteDuplicates(p);
}
}
};```

```/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:

ListNode *pDel=new ListNode(0);

ListNode *prev=pDel;
ListNode *curr=prev->next;

while(curr->next){
if(curr->next->val!=curr->val){
if(prev->next==curr)
prev=curr;
else
prev->next=curr->next;
}
curr=curr->next;
}

if(prev->next!=curr)
prev->next=curr->next;

return pDel->next;
}
};```
原文作者：AndyJee
原文地址: https://www.cnblogs.com/AndyJee/p/4467051.html
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。