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 ...
随机推荐
- ueditor上传图片配置
1 去ueditor文件夹下 找 ueidtors/dialogs/image/image.html -- 配置位置大概如下: 107 utils.domReady(function(){ 108 ...
- bzoj1907: 树的路径覆盖(树形DP)
一眼题... f[i][0]表示在i连接一个子树的最小值,f[i][1]表示在i连接两个子树的最小值,随便转移... 样例挺强的1A了美滋滋... UPD:学习了2314的写法之后短了好多T T #i ...
- luoguP5105 不强制在线的动态快速排序
emm 可重集合没用用.直接变成不可重复集合 有若干个区间 每个区间形如[L,R] [L,R]计算的话,就是若干个连续奇数的和.拆位统计1的个数 平衡树维护 加入一个[L,R],把相交的区间合并.之后 ...
- CSUST 四月选拔赛个人题解
这场比赛演的逼真,感谢队友不杀之恩 总结:卡题了赶紧换,手上捏着的题尽快上机解决 http://csustacm.com:4803/ 1113~1122 1113:六学家 题意:找出满足ai+aj=a ...
- UVA-1635 数学
UVA-1635 题意: 给定n个数a1,a2,a3.....an,依次求出相邻的两个数的和,最后成为一个数,问这个数模m的值与那些最初的数无关 例:a1,a2,a3, m=2 => a1+a2 ...
- 手脱PE Pack v1.0
1.PEID查壳 PE Pack v1.0 2.载入OD,一上来就这架势,先F8走着 > / je ; //入口点 -\E9 C49D0000 jmp Pepack_1.0040D000 004 ...
- oracle导入提示IMP-00013: 只有 DBA 才能导入由其他 DBA 导出的文件
oracle导入提示:IMP-00013: 只有 DBA 才能导入由其他 DBA 导出的文件IMP-00000: 未成功终止导入 解决办法:用户缺少导入权限,授予用户导入权限grant imp_ful ...
- 很好的脑洞题:dfs+暴力 Gym - 101128A Promotions
http://codeforces.com/gym/101128 题目大意:给你一个a,b,e,p.有e个点,p条有向边,每条边为(x,y),表示x->y,每次我们都取出一个入度为0的,并且一次 ...
- Git同时push到多个远程仓库
添加第二个远程地址时使用以下命令: git remote set-url --add origin git@github.com:morethink/programming.git 查看远程分支:gi ...
- GridControl详解(五)设置行备注和行号
备注显示设置 设置备注字段 显示结果: 可以写入按键事件F3,用以开关备注显示 private void Form4_KeyUp(object sender, KeyEventArgs e) { if ...