https://leetcode.com/problems/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.

思路:

需要两个指针,a用来纪录新链表,b用来遍历老链表,因为只有删除操作,需要一个flag标记ab段要不要删除。此外要注意头节点的处理。

AC代码:

 /**
* 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) {
if(head==NULL)
return NULL;
ListNode* newhead=new ListNode(); // to handle head
newhead->next=head;
ListNode* a=newhead,*b=newhead->next;
int now=head->val;
bool occur_flag=false;
while(b->next!=NULL){
if(b->next->val==now){
occur_flag=true;
}
else{
if(occur_flag==true){
a->next=b->next; //delete duplicate numbers
b=a; //update pointers
}
else{
a=b;
}
occur_flag=false;
now=b->next->val;
}
b=b->next;
}
if(occur_flag==true){
a->next=b->next; //delete duplicate numbers
b=a; //update pointers
}
else{
a=b;
}
return newhead->next;
}
};

LeetCode(82)题解: Remove Duplicates from Sorted List II的更多相关文章

  1. 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

  2. 【LeetCode练习题】Remove Duplicates from Sorted List II

    Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...

  3. 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  4. 【LeetCode】080. Remove Duplicates from Sorted Array II

    题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...

  5. leetcode笔记:Remove Duplicates from Sorted Array II

    一.题目描写叙述 二.解题技巧 这道题和Remove Duplicates from Sorted Array这道题是相似的.仅仅只是这里同意出现反复的数字而已,能够採用二分搜索的变种算法.仅仅只是增 ...

  6. LeetCode(26)题解:Remove Duplicates from Sorted Array

    https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove the ...

  7. LeetCode OJ 80. Remove Duplicates from Sorted Array II

    题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  8. LeetCode OJ:Remove Duplicates from Sorted Array II(移除数组中的重复元素II)

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  9. LeetCode(83)题解: Remove Duplicates from Sorted List

    https://leetcode.com/problems/remove-duplicates-from-sorted-list/ 题目: Given a sorted linked list, de ...

随机推荐

  1. 【前端学习笔记】2015-09-01 附 split()方法、readyState

    1.split():作用对象是一个字符串或者字符串对象,会要求设置两个参数(分割点(separator),分割出来的数量(number)),ps:1."2:3:4:5".split ...

  2. vue当中设置全局变量的方法

    import fn from 'fn' Vue.prototype.fn = fn; 调用的时候 this.fn.way

  3. 利用node搭建本地服务器调试代码

    在命令符中输入 npm install -g live-server安装live-server 安装完成后在项目文件夹下打开cmd,输入live-server启动

  4. Docker 架构详解

    Docker 的核心组件包括: Docker 客户端 - Client Docker 服务器 - Docker daemon Docker 镜像 - Image Registry Docker 容器 ...

  5. canvas绘制视频封面--摘抄

    一.需求:上传视频,同时截取视频某一帧作为视频的封面. 二.实现思路:利用canvas绘制图像的功能,绘制图像某一帧,这里绘制了第一帧,很简单就实现了. 三.代码: <!DOCTYPE html ...

  6. 仓鼠找sugar(lca)

    洛谷——P3398 仓鼠找sugar 题目描述 小仓鼠的和他的基(mei)友(zi)sugar住在地下洞穴中,每个节点的编号为1~n.地下洞穴是一个树形结构.这一天小仓鼠打算从从他的卧室(a)到餐厅( ...

  7. 构建伪Update服务器工具isr-evilgrade

    构建伪Update服务器工具isr-evilgrade   现在大部分软件都提供更新功能.软件一旦运行,就自动检查对应的Update服务器.如果发现新版本,就会提示用户,并进行下载和安装.而用户往往相 ...

  8. 第三章 poj 1064——关于带精度的二分法

    /* 题意:给定n个实数l[i],给定一个k 问:求最大的ans,使得sum l[i]/ans i=1 to n >=k,且ans最大*/ #include <iostream> # ...

  9. Chrom查看Flash缓存文件及Flash下载方法

    比如在优酷看视频时,或者熊猫直播,如果使用Flash进行播放的基本都会先缓存在本地,只不过这个缓存的名字后缀不叫flv,而是类似tmp这样:通常只要找到这个缓存文件,然后改为flv即可播放:如果出现文 ...

  10. Myeclipse 出现了Could not generate DH keypair 错误

    在myeclipse下安装svn插件,出现了Could not generate DH keypair,这么一个错误. 这个问题困扰了我半天时间,各种百度也找不到答案,或许是百度能力问题吧.百度出来的 ...