/** * 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】,把跟该节点值相同的节点都删掉