My C++ solution (6ms)


  • 0
    W
    class Solution {
    public:
        ListNode* deleteDuplicates(ListNode* head) {
            ListNode* newHead = new ListNode(0);
            newHead->next = head;
            ListNode* pre, *cur, *next, *tmp;
            int flag;
            pre = newHead;
            cur = pre->next;;
            while(cur){
                next = cur->next;
                flag = 0;
                while(next&&next->val==cur->val){
                    tmp=next;
                    next = next->next;
                    delete tmp;
                    flag=1;
                }
                if(flag==0){
                    pre = cur;
                    cur = next;
                }else{
                    delete cur;
                    pre->next = next;
                    cur = next;
                }
            }
            return newHead->next;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.