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.

这里对于链表去重是要将有重复的链表节点全部都去掉,一个都不能留,思路还是比较简单的,跟前面的I那一题实际上差不多,代码如下所示:

 /**
* 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) return NULL;
ListNode * helper = new ListNode(INT_MAX);
helper->next = head;
ListNode * prev = helper;
ListNode * curr = head;
while(curr){
if(curr->next && curr->val == curr->next->val){
ListNode * tmpNode = curr->next;
while(tmpNode->next && curr->val == tmpNode->next->val){
tmpNode = tmpNode->next;
}
prev->next = tmpNode->next;
curr = prev->next;
}else{
prev = curr;
curr = curr->next;
}
}
return helper->next;
}
};

LeetCode OJ:Remove Duplicates from Sorted List II(链表去重II)的更多相关文章

  1. [leetcode]83. Remove Duplicates from Sorted List有序链表去重

    Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1 ...

  2. [LeetCode]83. Remove Duplicates from Sorted List(排序链表去重)

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  3. LeetCode OJ Remove Duplicates from Sorted Array II

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

  4. LeetCode OJ——Remove Duplicates from Sorted List

    http://oj.leetcode.com/problems/remove-duplicates-from-sorted-list/ 链表的去重,要考虑链表的本质. #include <ios ...

  5. [leetcode]26. Remove Duplicates from Sorted Array有序数组去重(单个元素只出现一次)

    Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...

  6. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

  7. [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)

    https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...

  8. [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  9. [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项 II

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  10. 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++> 给出排序好的 ...

随机推荐

  1. Jmeter数据库mysql测试说明

    主要分3个步骤,详细操作步骤如下: 一.环境准备 1.下载mysql驱动,下载地址:https://dev.mysql.com/downloads/connector/j/,Select Operat ...

  2. Linux下修改时间

    修改linux的时间可以使用date指令 date命令的功能是显示和设置系统日期和时间. 输入date 查看目前系统时间. 修改时间需要 date -功能字符 修改内容 命令中各选项的含义分别为: - ...

  3. 两个java小练习

    网上看到的一个小练习,自己写了一个,但是时间限制并不符合,并且貌似还有些小问题,暂时放在这儿,代码格式什么的也不太规范. 1.班级排名时间限制: 1000ms 内存限制: 65536kB 描述 信息科 ...

  4. Oracle中验证非空的函数NVL(),NVL2()总结

    1.NVL()函数 NVL函数的格式如下: NVL(expr1,expr2) 含义是:如果oracle第一个参数为空那么显示第二个参数的值,如果第一个参数的值不为空,则显示第一个参数本来的值. 2 N ...

  5. 如何用纯 CSS 创作一只徘徊的果冻怪兽

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/VdOKQG 可交互视频 ...

  6. Java线程状态流转---线程

    说明:线程共包括以下5种状态.1. 新建状态(New)         : 线程对象被创建后,就进入了新建状态.例如,Thread thread = new Thread().2. 就绪状态(Runn ...

  7. vs LNK2019 无法解析的外部符号 ***,该符号在函数 WinMain 中被引用

    一般链接错误都是因为包含头文件与lib库不匹配(无导出函数.lib库的release debug版本混乱.库引用的优先级.编译器设置mt/mtd等等)造成的. 错误    LNK2019    无法解 ...

  8. C#生成PDF2019

    因接口生成Pdf推送, 工作需要进行Pdf生成,但网上生成Pdf的文档好少: 1.生成Pdf需要文件路径/内容  都可以配置 2.使用组件 itextsharp.dll 本人用版本:v2.0.5072 ...

  9. poj 2187:Beauty Contest(旋转卡壳)

    Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 32708   Accepted: 10156 Description Bes ...

  10. JasperReports实现报表调出excel

    一.利用工具iReport 创建task.jrxml 模板 并生成 task.jasper 文件 二.搭建工程导入以下jar包 commons-beanutils-1.9.2.jar commons- ...