leetcode-Remove Duplicates from Sorted List-83

来源:互联网 发布:淘宝白菜qq群 微信群 编辑:程序博客网 时间:2024/04/27 13:52

删除重复的节点。只需要遍历一遍。

/** * 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 head;        int tmp=head->val;        ListNode* p=head->next;        ListNode* pre=head;         while(p){            if(tmp==p->val){                ListNode* q=p->next;                pre->next=p->next;                p=q;            }            else{                tmp=p->val;                pre=p;                p=p->next;            }        }        return head;    }};


0 0