##题目
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.
##思路
本题采用递归的思路,代码清晰简单
##代码
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if (!head) return 0;
if (!head->next) return head;
int val = head->val;
ListNode* p = head->next;
if (p->val != val) {
head->next = deleteDuplicates(p);
return head;
} else {
while (p && p->val == val) p = p->next;
return deleteDuplicates(p);
}
}
};
标签:head,ListNode,val,Duplicates,List,Given,Remove,next,return
From: https://blog.51cto.com/u_15996214/6105914