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

遍历每一个节点,【新用一个指针p_i】,把跟该节点值相同的节点都删掉

posted @ 2015-07-30 09:28  *桔子*  阅读(140)  评论(0编辑  收藏  举报