83. Remove Duplicates from Sorted List

FreeeLinux發表於2017-02-21
Given a sorted linked list, delete all duplicates such that each element appear only once.

For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        ListNode* node = head;
        while(node != NULL && node->next != NULL){
            if(node->val == node->next->val){
                ListNode* tmp = node->next;
                node->next = tmp->next;
                delete tmp;
            }
            else
                node = node->next;
        }
        return head;
    }
};

相關文章