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) {
        ListNode *Head = (ListNode *)malloc(sizeof(ListNode));
        ListNode *tail = Head,*p1 = head;
        while(p1){
            while(p1->next&&p1->val == p1->next->val) p1 = p1->next;//去重
            tail->next = p1;
            tail = tail->next;
            p1 = p1->next;
        }
        return Head->next;
    }
};

 

posted @ 2015-03-14 12:10  SprayT  阅读(68)  评论(0编辑  收藏  举报