leetcode-----83. 删除排序链表中的重复元素

代码

/**
 * 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) {
        if (!head) return head;
        auto cur = head;
        for (auto p = head->next; p; p = p->next) {
            if (p->val != cur->val) cur = cur->next = p;
        }
        cur->next = nullptr;
        return head;
    }
};
posted @ 2020-07-15 09:19  景云ⁿ  阅读(71)  评论(0编辑  收藏  举报