83 Remove Duplicates from Sorted List
链接:https://leetcode.com/problems/remove-duplicates-from-sorted-list/
问题描述:
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.
Hide Tags Linked List
由于链表是排序好的,那么这个问题就变得是十分简单,注意内存问题就好了。
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(head==NULL||head->next==NULL) return head;
ListNode* pre=head,*p=head->next,*t;
while(p)
{
if(pre->val==p->val)
{
t=p;
p=p->next;
delete t;
}
else
{
pre->next=p;
p=p->next;
pre=pre->next;
}
}
pre->next=NULL;
return head;
}
};