83. Remove Duplicates from Sorted List

/**
 * 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 == NULL)   return head;
        ListNode* p = head;
        while (p->next) {
            if (p->next->val == p->val) {
                ListNode* t = p->next;
                p->next = t->next;
                delete t;
            }
            else
                p = p->next;
        }
        return head;
    }
};

 

posted @ 2018-11-24 15:44  JTechRoad  阅读(55)  评论(0编辑  收藏  举报