【Leetcode】之Remove Duplicates from Sorted List II
来源:互联网 发布:张大宇数据 编辑:程序博客网 时间:2024/06/14 18:37
一.问题描述
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given 1->2->3->3->4->4->5
, return 1->2->5
.
Given 1->1->1->2->3
, return 2->3
.
二.我的解题思路
删除链表节点的题目做得多了,说白了本题的考点就在于各种边界条件的处理。本题需要记录下duplicate number串的前一个数和后一个数。还要注意头结点也可能被删除。
测试通过的程序如下:
/** * 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) return NULL; ListNode* before=NULL;ListNode* after=NULL;ListNode* res=head; ListNode* curr=head;ListNode* next=head->next; ListNode* pri; while(curr && next){ while(next!=NULL && curr->val!=next->val ){ before=curr; curr=curr->next; next=curr->next; } if(next==NULL) break; while(next!= NULL && curr->val==next->val ){ curr=curr->next; next=curr->next; } if(next==NULL) after=NULL; after=next; if(before==NULL ) {res=after;curr=after; if(after==NULL) return NULL; next=curr->next;continue; } before->next=after; if(after==NULL) break; curr=after; next=curr->next; } return res; }};
0 0
- LeetCode: Remove Duplicates from Sorted List II
- [Leetcode] Remove Duplicates from Sorted List II
- LeetCode: Remove Duplicates from Sorted List II
- [Leetcode] Remove Duplicates from Sorted List II
- [leetcode] Remove Duplicates from Sorted List II
- [LeetCode] Remove Duplicates from Sorted List II
- [leetcode] Remove Duplicates from Sorted List II
- [LeetCode]Remove Duplicates from Sorted List II
- [leetcode]Remove Duplicates from Sorted List II
- LeetCode-Remove Duplicates from Sorted List II
- [leetcode] Remove Duplicates from Sorted List II
- LeetCode - Remove Duplicates from Sorted List II
- LeetCode | Remove Duplicates from Sorted List II
- LeetCode:Remove Duplicates from Sorted List II
- 【leetcode】Remove Duplicates from Sorted List II
- Leetcode: Remove Duplicates from Sorted List II
- leetcode Remove Duplicates from Sorted List II
- [LeetCode] Remove Duplicates from Sorted List II
- 利用Qt的QDateTimeEdit设置起始时间(即开始时间大于结束时间)
- LINUX-内核-中断分析-中断向量表(1)-x86
- 大数相加+斐波那契数列(1)
- 转义字符以及类型的求取
- MySQL 1064 错误解决方法
- 【Leetcode】之Remove Duplicates from Sorted List II
- android sensor架构详解
- 2016/7/18
- 集合框架
- PHP分页函数代码(简单实用型)
- php使用cookie来保存用户登录信息
- Android绘图那些事儿(上)
- OpenGL-绘制点、线、面
- 网络基础篇——IP数据报格式及路由器报表算法