c_cpp 83.从排序列表中删除重复项

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 83.从排序列表中删除重复项相关的知识,希望对你有一定的参考价值。

//Runtime: 12 ms, faster than 93.68% 

class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        if(head == nullptr) return nullptr;
        
        ListNode pseudo_head(0);
        pseudo_head.next = head;
        ListNode* prev = pseudo_head.next;
        ListNode* cur = prev->next;
        
        while(cur){
            if(prev->val == cur->val)
                prev->next = cur->next;
            else
                prev = prev->next;
            cur = cur->next;
        }
        
        return pseudo_head.next;
    }
};
//Runtime: 8 ms, faster than 100.00%

class Solution {
public:
    ListNode *deleteDuplicates(ListNode *head) {
        ListNode* cur = head;
        while (cur) {
            while (cur->next && cur->val == cur->next->val)
                cur->next = cur->next->next;
            cur = cur->next;
        }
        return head;
    }
};
//Runtime: 8 ms, faster than 100.00% 

class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        ListNode* cur = head;
        while(cur && cur->next){
            if(cur->val == cur->next->val){
                ListNode* tmp = cur->next;
                cur->next = cur->next->next;
                delete tmp;
            }
            else
                cur = cur->next;
        }
        return head;
    }
};

以上是关于c_cpp 83.从排序列表中删除重复项的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 从已排序的链接列表中删除重复项

c_cpp 从排序列表中删除重复项IIhttp://oj.leetcode.com/problems/remove-duplicates-from-sorted-list-ii/

c_cpp 26.从排序数组中删除重复项 - DifficultyEasy - 2018.9.5

java 82.从排序列表II(递归).java中删除重复项

java 82.从排序列表II(递归).java中删除重复项

java 82.从排序列表II(递归).java中删除重复项