[leetcode] Remove Duplicates from Sorted List

来源:互联网 发布:java系统日志管理 编辑:程序博客网 时间:2024/06/06 07:15

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.


class Solution {public:    ListNode *deleteDuplicates(ListNode *head) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        int count=0;        ListNode *p,*q;        if(!head)return NULL;        p=head;        while(p->next){            if(p->val==p->next->val){                count++;                if(count>1){                    q=p->next;                    p->next=p->next->next;                    delete q;                }            }else{                count=0;                p=p->next;            }        }        return head;    }};


原创粉丝点击