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(NULL != head)
{
ListNode* ret = new ListNode(head->val);
ListNode* newList = ret;
ListNode* cur = head;
while(NULL != cur)
{
if(cur->val != newList->val)
{
newList->next = new ListNode(cur->val);
newList = newList->next;
}
cur = cur->next;
}
return ret;
}
return NULL;
}
};
不递归就不超时了。。。