欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

LeetCode 83. Remove Duplicates from Sorted List

程序员文章站 2022-07-14 08:40:04
...

83. Remove Duplicates from Sorted List

LeetCode 83. Remove Duplicates from Sorted List

class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        ListNode dummy(-1);
        dummy.next = head;
        ListNode* walk = &dummy;
        ListNode* stop = &dummy;
        int val1 = 0xefffffff;
        while(walk->next !=NULL){
            walk = walk->next;
            int val2 = walk->val;
            if(val1 !=val2){
                stop->next = walk;
                stop = walk;
                val1 = val2;
            }   
        }
        if(stop != walk)
            stop->next = walk->next;
        return dummy.next;
    }
};