题目:
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.
比较简单就是删除相同的元素,代码如下:
/**
* 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 == NULL)return head;
ListNode * front = head,*tail = head->next;
while(tail != NULL){
if(tail->val == front->val){
front->next = tail->next;
}
else{
front = tail;
}
tail = tail->next;
}
return head;
}
};