LeetCode——remove-duplicates-from-sorted-list-ii
Question
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given1->2->3->3->4->4->5, return1->2->5.
Given1->1->1->2->3, return2->3.
Solution
判断当前节点和下一个节点是否相等,同时在开头添加了一个节点,因为开始的节点可能就是重复的,会被删除。
Code
/**
* 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) {
ListNode* pNode = head;
ListNode* tmp = new ListNode(0);
tmp->next = pNode;
ListNode* pre = tmp;
while (pNode != NULL) {
bool flag = false;
while (pNode->next != NULL && pNode->val == pNode->next->val) {
flag = true;
pNode = pNode->next;
}
if (flag) {
pre->next = pNode->next;
pNode = pNode->next;
} else {
pre = pNode;
pNode = pNode->next;
}
}
return tmp->next;
}
};
LeetCode——remove-duplicates-from-sorted-list-ii的更多相关文章
- Leetcode: Remove Duplicates from Sorted List II 解题报告
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
- [LeetCode] Remove Duplicates from Sorted List II 移除有序链表中的重复项之二
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [Leetcode] Remove Duplicates From Sorted Array II (C++)
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Duplicates from Sorted Array II [27]
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- [leetcode]Remove Duplicates from Sorted Array II @ Python
原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 题意: Follow up for &quo ...
- [LeetCode] Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [leetcode]Remove Duplicates from Sorted List II @ Python
原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ 题意: Given a sorted link ...
- LeetCode::Remove Duplicates from Sorted List II [具体分析]
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
随机推荐
- git学习(5)分支管理(续)
git学习(5)分支管理(续) 1.解决冲突 冲突的产生 如我们在新建分支和原来master分支上对同一文件做了修改并提交,在合并分支的时候就会遇到冲突 比如我新建了分支myBranch,在这个分支上 ...
- Android Handler 的使用
Android UI 操作是线程不安全的.我们只能在UI线程或者说主线程中修改UI.试想多个Thread操作同一个UI,可能引起不一致.UI 线程的主要工作是:UI界面更新显示,各个控件的交互等等.一 ...
- c#自定义控件做漂亮的列表
效果图如下: 完整项目代码下载: 点击下载
- HTTP Transaction Delays
w客户端.服务器超载 HTTP The Definitive Guide 与建立TCP连接以及传输请求和相应报文的时间相比,事务处理的时间是很短的.除非客户端或服务器超载或正在处理复杂的动态资源,否则 ...
- multi-paradigm
w范式 https://developer.mozilla.org/en-US/docs/Web/JavaScript https://developer.mozilla.org/zh-CN/docs ...
- win下自动sftp脚本定时下载文件
缘起一个BA与客户交流的软件.但因为数据不能通过系统直连的方式进行获取. 对方只提供每天一份全量数据到指定文件夹下,我方自动通过sftp的方式去拉取. 一个只有简单几行的操作,想必肯定是不可能需写程序 ...
- CF #301 E:Infinite Inversions(逆序数,树状数组)
A-Combination Lock B-School Marks C-Ice Cave D-Bad Luck Island E-Infinite Inversions E:Infini ...
- cookie和session的自我介绍
Cookie是什么? cookie说的直白点就是保存在用户浏览器端的一个键值对,举个例子,你现在登录了京东商城,你把浏览器关闭之后,你再打开京东,你还是可以对你的账户继续操作,已经购买的商品,订单都是 ...
- karma安装
Last login: Sat Jun :: on ttys000 ➜ ~ cd /Users/wangyizhe/Projects/work/smartcmp/services/new-yacmp/ ...
- Linux修改信息
修改时间 sudo date -s MM/DD/YY //修改日期 sudo date -s hh:mm:ss //修改时间 在修改时间以后,修改硬件CMOS的时间 sudo hwclock --sy ...