BM16. 删除有序链表中重复的元素-II
描述
示例1
输入:
{1,2,2}
输出:
{1}
示例2
输入:
{}
输出:
{}
C++ 解法, 执行用时: 2ms, 内存消耗: 328KB, 提交时间: 2020-11-23
/** * struct ListNode { * int val; * struct ListNode *next; * }; */ class Solution { public: /** * * @param head ListNode类 * @return ListNode类 */ ListNode* deleteDuplicates(ListNode* head) { // write code here /*if (!head || !head->next) return head; if(head->val==head->next->val){ return nullptr; } ListNode *start = head; while (start && start->next) { if (start->val == start->next->val) { ListNode *tmp = start->next; start->next = start->next->next; delete tmp; } else start = start->next; } return head;*/ //https://blog.csdn.net/No_one_z/article/details/109634379 if(head==nullptr || head->next==nullptr)return head; ListNode* newHead = new ListNode(1); newHead->next=head; ListNode*pre = newHead; ListNode*cur = head; int count=0; while(cur && cur->next){ if(cur->val==cur->next->val){ cur=cur->next; count++; }else{ if(count > 0){ pre->next=cur->next; cur=cur->next; count =0; }else{ pre=pre->next; cur=cur->next; } } } if(count > 0){ pre->next=cur->next; } return newHead->next; } };
C++ 解法, 执行用时: 2ms, 内存消耗: 328KB, 提交时间: 2020-11-22
/** * struct ListNode { * int val; * struct ListNode *next; * }; */ class Solution { public: /** * * @param head ListNode类 * @return ListNode类 */ ListNode* deleteDuplicates(ListNode* head) { // write code here if(head==nullptr||head->next==nullptr) return head; ListNode newhead(0); newhead.next=head; ListNode *pnode=&newhead; while(pnode->next) { ListNode *ptemp=pnode->next; bool flag=false; while(ptemp&&ptemp->next&&ptemp->val==ptemp->next->val) { ptemp=ptemp->next; flag=true; } if(flag) pnode->next=ptemp->next; else pnode=pnode->next; } return newhead.next; } };
C++ 解法, 执行用时: 2ms, 内存消耗: 332KB, 提交时间: 2021-05-24
/** * struct ListNode { * int val; * struct ListNode *next; * }; */ class Solution { public: /** * * @param head ListNode类 * @return ListNode类 */ ListNode* deleteDuplicates(ListNode* head) { // write code here ListNode *dummy = new ListNode(0);//省去判断链表头重复的情况 dummy->next = head; ListNode *cur=head,*pre=dummy,*q;//记录前一个没重复的节点和当前节点 if(head==NULL||head->next==NULL){ return head; } while(cur&&cur->next){//后面元素少于2个则不会重复 bool repeat = 0;//是否重复标志位 while(cur->next && cur->next->val==pre->next->val){//存在下一节点并且下一节点的值与pre后面的值相同时(即接下来要判断是否重复的节点值) repeat = 1; cur = cur->next; } if(repeat){//当有重复时前一个没重复的节点指向末尾重复节点的下一位(可能为下一位) pre->next = cur->next; } else{//否则没重复节点前移 pre = cur; } cur = cur->next; } return dummy->next; } };
C++ 解法, 执行用时: 2ms, 内存消耗: 332KB, 提交时间: 2021-05-22
/** * struct ListNode { * int val; * struct ListNode *next; * }; */ class Solution { public: /** * * @param head ListNode类 * @return ListNode类 */ ListNode* deleteDuplicates(ListNode* head) { // write code here ListNode* dummy=new ListNode(233); dummy->next=head; ListNode* p=head,*pre=dummy,*q; if(head==NULL||head->next==NULL) return head; while(p&&p->next) { q=p; while(q&&q->val==p->val) q=q->next; if(q==NULL) pre->next=NULL; if(q==p->next) { pre->next=p; pre=p; } p=q; } if(p&&p->next==NULL) pre->next=p; return dummy->next; } };
C++ 解法, 执行用时: 2ms, 内存消耗: 332KB, 提交时间: 2020-11-21
/** * struct ListNode { * int val; * struct ListNode *next; * }; */ class Solution { public: /** * * @param head ListNode类 * @return ListNode类 */ ListNode* deleteDuplicates(ListNode* head) { // write code here if(head==nullptr || head->next==nullptr) return head; ListNode* dummy = new ListNode(0); dummy->next=head; ListNode* p=head; ListNode* pre=dummy; while(p!=nullptr && p->next!=nullptr) { if(p->val==p->next->val) { ListNode* Next=p; while(Next->next!=nullptr && (Next->val==Next->next->val)) Next=Next->next; pre->next=Next->next; while(p!=pre->next) { Next=p->next; delete p; p=Next; } } else{ p=p->next; pre=pre->next; } } return dummy->next; } };