【剑指 offer】JZ56 删除链表中重复的结点 C++

JZ56 删除链表中重复的结点

/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
        val(x), next(NULL) {
    }
};
*/
class Solution {
public:
    ListNode* deleteDuplication(ListNode* pHead)
    {
        ListNode* head = new ListNode(0);
        ListNode* p = head;
        p->next = pHead;
        while(p && p->next) {
            ListNode* q = p->next;
            while(q->next && q->val == q->next->val) q = q->next;
            if(p->next == q) p = q;
            else p->next = q->next;
        }
        return head->next;
    }
};

你可能感兴趣的:(剑指offer,链表,面试)