Remove Duplicates from Sorted List

塵埃092發表於2014-08-17
/**
 * 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 )
            return NULL;
        else
        {
            ListNode *deal, *next;
            deal = head;
            while( deal )
            {
                next = deal->next;
                while( next )
                {
                    if( next->val == deal->val )
                        next = next->next;
                    else
                        break;
                }
                deal->next = next;
                deal = deal->next;
            }
            return head;
        }
    }
};

相關文章