leetcode83 C++ 12ms 删除有序链表中的重复元素

/**
 * 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) {
        ListNode* res = head;
        ListNode* diff = nullptr;
        while(head != nullptr){
            diff = head->next;
            while(diff !=nullptr && diff->val == head->val){
                diff = diff->next;
            }
            head->next = diff;
            head = diff;
        }
        return res;
        
    }
};

猜你喜欢

转载自www.cnblogs.com/theodoric008/p/9361877.html
今日推荐