Remove Duplicates from Sorted List

Description:

Given a sorted linked list, delete all duplicates such that each element appear only once.

For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.

Code:

 1 ListNode* deleteDuplicates(ListNode* head) {
 2         if (head==NULL)
 3             return NULL;
 4             
 5         ListNode* p = head;
 6         ListNode* pre = NULL;
 7         
 8         pre = p;
 9         p = p->next;
10         
11         while (p)
12         {
13             if (p->val == pre->val)
14             {//删除p结点
15                 pre->next = p->next;
16                 delete p;
17                 p = pre->next;
18             }
19             else
20             {
21                 pre = p;
22                 p = p->next;
23             }
24         }
25         return head;
26     }
原文地址:https://www.cnblogs.com/happygirl-zjj/p/4586151.html