Leetcode-删除排序链表中的重复元素
17.删除排序链表中的重复元素
题目内容:
代码及思路:
/**
* 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) {
ListNode* res=head;
if(head==nullptr)
return nullptr;
while(res!=nullptr&&res->next!=nullptr)
{
while(res->next!=nullptr&&res->val==res->next->val)
{
res->next=res->next->next;
}
res=res->next;
}
return head;
}
};