删除链表中重复的结点
题目描述
在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5
思路:设置了一个链表头的标记节点,解决链表头重复需要删除的情况
代码:
ListNode* deleteDuplication(ListNode* pHead)
{ if(pHead == NULL || pHead->next == NULL) return pHead; ListNode *first = new ListNode(-1); first->next = pHead; ListNode* tmp = pHead; ListNode* tmpre = first; while(tmp != NULL &&tmp->next ) { if(tmp->val == tmp->next->val) { while(tmp->next && tmp->next->val == tmp->val) tmp = tmp->next; tmpre->next = tmp->next; tmp = tmpre -> next; } else { tmpre = tmp; tmp = tmp->next; } } return first->next; }