83. Remove Duplicates from Sorted List

Problem:

Given a sorted linked list, delete all duplicates such that each element appear only once.

Example 1:

Input: 1->1->2
Output: 1->2

Example 2:

Input: 1->1->2->3->3
Output: 1->2->3

思路

Solution (C++):

ListNode* deleteDuplicates(ListNode* head) {
    ListNode*p = head;
    while (p && p->next) {
        if (p->val == p->next->val) {
            if (p->next->next) {
                p->next = p->next->next;
            } else
                p->next = NULL;
        } else
            p = p->next;
    }
    return head;
}

性能

Runtime: ms  Memory Usage: MB

思路

Solution (C++):


性能

Runtime: ms  Memory Usage: MB