1、题目描述
2、题目分析
链表的题,主要注意指针即可。
3、代码
1 ListNode* deleteDuplicates(ListNode* head) { 2 if (head == NULL || head->next == NULL) 3 return head; 4 5 ListNode *dummy = new ListNode(0); 6 dummy->next = head; 7 ListNode *p = head; 8 ListNode *pre = dummy; 9 10 while (p != NULL) { 11 ListNode *tmp = p->next; 12 if (tmp == NULL) 13 break; 14 while (tmp->val == p->val) { 15 tmp = tmp->next; 16 if (tmp == NULL) { 17 pre->next = NULL; 18 break; 19 } 20 } 21 22 if (p->next == tmp) { 23 pre = p; 24 }else { 25 pre->next = tmp; 26 } 27 p = tmp; 28 29 } 30 return dummy->next; 31 32 }