删除重复节点列表中的,假设所有val如果仅仅是为了保持一个非常easy。应承担重复val节点被删除话。要保持pre节点,每当你想保存这pre问题节点。应该head节点可以被取出,好了,没问题边境控制。
class Solution { public: ListNode *deleteDuplicates(ListNode *head) { if(head == NULL || head->next == NULL) return head; ListNode *pre = NULL, *start = head, *end; while(start){ while(start&&start->next&&start->val != start->next->val){ pre = start; start = start->next; } end = start; while(end&&end->next&&end->val == end->next->val) end = end->next; if(end!=start&&end){ if(pre) pre->next = end->next; else{ head = end->next; } }else{ break; } start = end->next; } return head; } };