83. Remove Duplicates from Sorted List

来源:互联网 发布:windows 苹方字体 otf 编辑:程序博客网 时间:2024/05/24 06:31



/** * 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)        {                    ListNode* p = head;            ListNode* q = p->next;                        while (q)            {                if (p->val == q->val){                    p->next = q->next;                    free(q);                    q = p->next;                }                else{                    p = p->next;                    q = q->next;                }                            } //while                }        return head;        */                        if (!head) return head;                ListNode* p1 = head;        ListNode* p2 = head;                while(p1)        {            p2 = p2->next;            while(p2 && p1->val == p2->val)            {                ListNode* tmp = p2->next;                //free(p2);                p2 = tmp;            }            p1->next = p2;            p1 = p2;        }                return head;            }};


原创粉丝点击