/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {
public:// 今天手撕一下这道题目// 只要有重复, 我们就一个不留,全删了。// 怎么做呢?ListNode* deleteDuplicates(ListNode* head) {// 首先if(head == NULL || head -> next == NULL){return head;}// 第一个元素也可能重复// 比如 1->1->1 return NULL// 因此需要设置哑节点ListNode* dummy = new ListNode(0);dummy -> next = head;// 然后, 我们需要一个不包含重复元素的尾巴节点 end// end -> 指向下一个不是重复的节点ListNode* end = dummy; // 因为head 有可能也是重复的// 我们接下来要做的就是判断end -> 所指的之后的节点去重while(end != NULL && end -> next != NULL){// 判断时,需要一个节点与他的后一个节点比较ListNode* cur = end -> next;if(cur -> next == NULL || cur -> val != cur -> next -> val ){end = cur;}// 有重复节点else{while(cur -> next != NULL && cur -> val == cur -> next -> val){cur = cur -> next;}end -> next = cur -> next;}}return dummy -> next;}
};