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.
这道题特别简单,其实关于链表的题都没有什么技巧,就是一堆的指针指来指去.
/**
* 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 *pre,*now,*Head;
if(!head||!head->next)return head;
Head=new ListNode(-);
Head->next=head;
pre=Head;
now=head;
while(now&&now->next)
{
if(now->val == now->next->val)
{
while(now->next && now->val == now->next->val)
{
now=now->next;
}
pre->next=now->next;
now=now->next;
}
else
{
pre=now;
now=now->next;
}
}
head=Head->next;
delete(Head);
return head;
}
};
Remove Duplicates from Sorted List II——简单的指针问题的更多相关文章
- 【链表】Remove Duplicates from Sorted List II(三指针)
题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct ...
- 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 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 Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
- 48. Remove Duplicates from Sorted List && Remove Duplicates from Sorted List II
Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such that each ...
- 【LeetCode练习题】Remove Duplicates from Sorted List II
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
- Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II
以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置 一:Remove Element Given an array and a value, remove a ...
- LeetCode之“链表”:Remove Duplicates from Sorted List && Remove Duplicates from Sorted List II
1. Remove Duplicates from Sorted List 题目链接 题目要求: Given a sorted linked list, delete all duplicates s ...
随机推荐
- 洛谷U14667 肝活动【比赛】 【状压dp】
题目描述 Yume 最近在玩一个名为<LoveLive! School idol festival>的音乐游戏.他之所以喜欢上这个游戏,是因为这个游戏对非洲人十分友好,即便你脸黑到抽不出好 ...
- restful风格请求及都是 / 的请求及参数也在请求的/中
前台请求的样式: http://localhost:8080/item/88909 其中参数就是最后的 商品id号 88909 后台Controller中取出参数的方法: @Controller p ...
- jsp 的 3 个编译指令
JSP 的编译指令是通知 JSP 引擎的消息,它不直接生成输出. 常见的编译指令有如下三个: 1.page:该指令是针对当前页面的指令 2.include:用于指定包含另一个页面 3.taglib:用 ...
- navicat for mysql 导出数据的坑
navicat 选择转储结构和数据的时候,生成的 sql 文件会比较大,因为每一条数据都会生成一条 sql 语句,所以会导致 使用 source 还原的时候会很慢很慢很慢, 而使用 mysqldump ...
- SpringBoot(三) :Spring boot 中 Redis 的使用
前言: 这一篇讲的是Spring Boot中Redis的运用,之前没有在项目中用过Redis,所以没有太大的感觉,以后可能需要回头再来仔细看看. 原文出处: 纯洁的微笑 SpringBoot对常用的数 ...
- OpenCV---模糊操作
推文:图像平滑处理(归一化块滤波.高斯滤波.中值滤波.双边滤波) 推文:图像的平滑与滤波 模糊操作 三种模糊操作方式 均值模糊 中值模糊 自定义模糊(可以实现上面两种模糊方式) 原理: 图像处理:基础 ...
- mysql数据库使用sql命令窗口查询的数据,改成sql语句导入到mysql数据库中
1.查询语句为select * from t_table;导出的数据格式如下: 2.将数据文本备份,然后使用NOTEPAD++打开,然后只拷贝数据到新建txt中,然后进行如下替换: 1)将“ | ”分 ...
- 2015/10/9 Python核编初级部分学习总结
终于在十一长假之后的两天看完了<Python核心编程>的初级部分.虽然到后来两章,类和环境看得越来越慢,越来越难以理解.很多东西只能靠强记,也没办法真正掌握了,我想了想,还是不强迫自己去背 ...
- Linux网络知识
在思科上面模拟一下数据包的传递过程:一般上网使用的协议是tcp 交换机是一个2层的设备,它和Ip地址是没有关系的. 交换机上主要处理的是硬件地址(MAC),它只能分析到硬件地址,再到IP地址它就不管了 ...
- 【BZOJ4870】组合数问题(计数DP,快速幂)
题意: 1 ≤ n ≤ 10^9, 0 ≤ r < k ≤ 50, 2 ≤ p ≤ 2^30 − 1 思路:From http://blog.csdn.net/qq_33229466/artic ...