leetcode 83 Remove Duplicates from Sorted List

/**
 * 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||!head->next) return head;
        ListNode* pre=head,*node=head->next;
        while(node) {
            if(node->val==pre->val) {
                ListNode* tmp=node;
                node=node->next;
                delete tmp;
            }
            else {
                pre->next=node;
                pre=node;
                node=node->next;
            }
        }
        pre->next=nullptr;//1 1 2 3 3
        return head;
    }
};
原文地址:https://www.cnblogs.com/LiuQiujie/p/12613460.html