leetcode-83-链表题-删除排序链表中的重复元素 发表于 2020-03-08 | 分类于 数据结构与算法 题目 解法123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051/** * 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 || !head->next) return head; ListNode *a = head; ListNode *b = head->next; while(b){ if(a->val == b->val){ if(!b->next) a->next = b->next; b = b->next; }else{ a->next = b; a = a->next; b = b->next; } } return head; }};// 直接法class Solution {public: ListNode* deleteDuplicates(ListNode* head) { if(!head || !head->next) return head; ListNode *ptr{head}; while(ptr->next){ if(ptr->val == ptr->next->val){ ptr->next = ptr->next->next; }else{ ptr = ptr->next; } } return head; }};