[leetcode笔记] Remove Duplicates from Sorted List II
问题描述:
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
.
解题笔记:
折腾了好多次,这次先写好很多测试用例,刚开始总是不过,总有情况未处理好,修改了多次后终于写成:
1 class Solution { 2 public: 3 ListNode *deleteDuplicates(ListNode *head) { 4 if (head==NULL || head->next==NULL) 5 return head; 6 7 ListNode *pDummyHead = new ListNode(0); 8 pDummyHead->next = head; 9 10 ListNode *pNewHead = head; 11 ListNode *p = head; 12 ListNode *pLast = pDummyHead; 13 14 bool bFoundHead = false; 15 while (p->next) 16 { 17 int nLastValue = p->val; 18 ListNode *pSub = p->next; 19 if (p->val != pSub->val) 20 { 21 if (bFoundHead==false) 22 { 23 bFoundHead=true; 24 pNewHead = p; 25 } 26 pLast = p; 27 p = p->next; 28 continue; 29 } 30 while (pSub && (pSub->val == nLastValue)) 31 { 32 ListNode *pToDelete = pSub; 33 pSub = pSub->next; 34 delete pToDelete; 35 } 36 ListNode *pToDelete = p; 37 p = pSub; 38 delete pToDelete; 39 pLast->next = pSub; 40 if (p==NULL) 41 break; 42 } 43 if (bFoundHead==false) 44 { 45 pNewHead = p; 46 } 47 delete pDummyHead; 48 return pNewHead; 49 } 50 };
提交OJ,Accepted!
转载于:https://www.cnblogs.com/lequ/p/3885935.html
[leetcode笔记] Remove Duplicates from Sorted List II相关推荐
- LeetCode 80. Remove Duplicates from Sorted Array II
80. Remove Duplicates from Sorted Array II My Submissions QuestionEditorial Solution Total Accepted: ...
- LeetCode之Remove Duplicates from Sorted Array II
1.题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ...
- 【Leetcode】Remove Duplicates from Sorted List II
问题: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct ...
- Leetcode 82. Remove Duplicates from Sorted List II
利用一个虚拟头节点,和维护一个前置节点. # Definition for singly-linked list. # class ListNode: # def __init__(self, x): ...
- 【Leetcode】Remove Duplicates from Sorted Array II
题目:对上一题的延伸,每个数字可以出去2次. 思路:还是设置两个下标.第一个lenxb标记已去重的地方,第二个i标记待处理的位置.每次比较时,比较lenxb和lenxb-1两个位置,如果都相等,说明出 ...
- Leetcode OJ: Remove Duplicates from Sorted Array I/II
删除排序数组重复元素,先来个简单的. Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates i ...
- LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] c++
LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...
- [leetcode]83.Remove Duplicates from Sorted List
题目 Given a sorted linked list, delete all duplicates such that each element appear only once. Exampl ...
- [勇者闯LeetCode] 83. Remove Duplicates from Sorted List
[勇者闯LeetCode] 83. Remove Duplicates from Sorted List Description Given a sorted linked list, delete ...
最新文章
- 使用程序解决一道逻辑推理题
- react map循环生成的button_关于Vue和React的一些对比及个人思考(中)
- 循环的时候去删除集合中的元素 java.util.ConcurrentModificationException
- linux db2sysc 内存,db2sysc进程占用linux内存持续增长,请各位指点。
- controller属于哪一层_从数字一到十,在人们心中哪一个数字最为吉利?有什么原因?...
- linux查看虚拟化版本,4.15. 虚拟化 (机器翻译版本)
- UAC白名单解决每次启动都弹出UAC对话框(不用关闭UAC)
- 一文了解滴滴与蚂蚁金服开源共建的SQLFlow
- Apache POI Excel固定(冻结)单元格
- MT7921方案WIFI6无线网卡驱动编译方法
- 中国长租公寓与住房租赁领域未来八大趋势和创新探讨
- python对英雄皮肤进行图片采集~
- ExtJS教程(5)---Ext.data.Model之高级应用
- MyEclipse热部署----使用工具 JRebel
- 【Pyecharts | Map3D】带光影效果的3D地图 | 深圳地区二手房房价地图~
- 结对项目-小学生四则运算系统(GUI)
- 1.0 BS结构软件类型介绍
- 【T3】用友畅捷通特殊行业的期间损益结转设置
- Llinux系统下安装GNOME桌面
- 蚂蚁搬沙(2017佛山市选拔初中组)
热门文章
- ubuntu install opengl
- MySQL连接问题【如何解决MySQL连接超时关闭】
- Fedora 16 硬盘安装体会
- 《算法导论》(一)--插入排序与合并排序
- php二叉树 排列,PHP 如何实现用户二叉树排序需求
- leetcode算法题--比特位计数
- linux恢复出厂设置_怎么恢复tp-link路由器出厂设置 恢复tp-link出厂设置方法【详解】...
- 绘制简单的正太分布图
- 超越 一切还刚刚开始
- 《JavaScript面向对象精要》——第1章 原始类型和引用类型1.1 什么是类型