标签:leetcode
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.
对于这个删除链表中的重复元素但是对于重复元素需要保留一个,前面遇到的的重复元素是出现在前面还是中间这个问题就没有了,依次执行删除即可。
runtime:16ms
/**
* 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 NULL;
ListNode *tmp=head;
while(head!=NULL)
{
if(head->next&&head->val==head->next->val)
{
head->next=head->next->next;
}
else
{
head=head->next;
}
}
return tmp;
}
};
LeetCode83:Remove Duplicates from Sorted List
标签:leetcode
原文地址:http://blog.csdn.net/u012501459/article/details/46415445