Remove Duplicates from Sorted List I

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) return NULL;
13         ListNode* node = head;
14         while(node->next) {
15             if(node->val == node->next->val) {
16                 ListNode* tmp = node->next;
17                 node->next = node->next->next;
18                 delete tmp;
19             }
20             else {
21                 node = node->next;
22             }
23         }
24         return head;
25     }
26 };
原文地址:https://www.cnblogs.com/zhengjiankang/p/3646429.html