本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode专题的第51篇文章,我们来看LeetCode第82题,删除有序链表中的重复元素II(Remove Duplicates from Sorted List II). 这题官方给出的难度是Medium,点赞1636,反对107,通过率在36.3%左右.根据我们之前的分析,这题的难度适中,并且质量很高,好评如潮.实际上也的确如此,这题算法本身并不难,但是想要完整没有bug地实现并不容易,我们一起来看看. 题意 给定一个有
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A =[1,1,1,2,2,3], Your function should return length =5, and A is now[1,1,2,2,3]. 题意:可以保留一个重复的元素. 思路:第一种是和Remove duplicates from sorte
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example,Given input array A =[1
Given a sorted linked list, delete all duplicates such that each element appear only once. For example,Given1->1->2, return1->2.Given1->1->2->3->3, return1->2->3. 思路:遍历链表.比较相邻的结点,若是有相等的情况,则cur不动,而将其后继从其next变为next->next:没有,则正常