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.
1 /**
2 * Definition for singly-linked list.
3 * struct ListNode {
4 * int val;
5 * ListNode *next;
6 * ListNode(int x) : val(x), next(NULL) {}
7 * };
8 */
9 class Solution {
10 public:
11 ListNode* deleteDuplicates(ListNode* head) {
12 if(head == NULL){
13 return head;
14 }
15
16
17 ListNode* l1 = head;
18
19 while(l1 && l1->next)
20 {
21 if(l1->val != l1->next->val){
22 l1 = l1->next;
23 }else{
24 ListNode* p = l1->next->next;
25 l1->next = p;
26 }
27
28 }
29 return head;
30 }
31 };