remove duplicates from sorted list

    ListNode *deleteDuplicates(ListNode *head) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if( head == NULL ||head->next == NULL ) return head;
        ListNode * root = new ListNode (-1 );
        root -> next = head;
        ListNode *ptr = head ;
        while( ptr != NULL )
        {
            ptr = ptr->next;
            while( ptr!= NULL && ptr->val == head -> val)              
                ptr = ptr -> next;  
            head -> next = ptr;
            head = head->next;           
        }
        return root->next;
        
    }

 

posted on 2013-07-04 09:20  jumping_grass  阅读(117)  评论(0)    收藏  举报

导航