Small 10 lines of code cpp

programming
Tags: #<Tag:0x00007f182c071778>

#1

ListNode* Solution::deleteDuplicates(ListNode* A) {
ListNode* t=A;
if(!t->next)return t;
while(t->next){
while(t->val==t->next->val){
t->next=t->next->next;
if(!t->next)return A;
}
t=t->next;
}
return A;
}


#2

what is the difference between your code and my codeā€¦there is a runtime error showed for my code

ListNode* Solution::deleteDuplicates(ListNode* A)
{
struct ListNode *head = A;
if(head==NULL) return NULL;
if(head->next == NULL) return A;
while(head != NULL)
{
while(head->val==(head->next)->val)
{
head->next=(head->next)->next;
if(!head -> next) return A;
}
head=head->next;
}
return A;
}


#3

A simple C++ implementation
ListNode* Solution::deleteDuplicates(ListNode* A) {
if(A->next==NULL){
return A;
}
ListNode *i=A;
while(i->next != NULL){
if(i->val == i->next->val){
i->next=i->next->next;
}
else{
i=i->next;
}

}
return A;

}