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的更多相关文章

  1. Leetcode: Remove Duplicates from Sorted List II 解题报告

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

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

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

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

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

  4. [Leetcode] Remove Duplicates From Sorted Array II (C++)

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

  5. [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素

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

  6. [LeetCode] Remove Duplicates from Sorted Array II [27]

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

  7. [leetcode]Remove Duplicates from Sorted Array II @ Python

    原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 题意: Follow up for &quo ...

  8. [LeetCode] Remove Duplicates from Sorted List II

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

  9. [leetcode]Remove Duplicates from Sorted List II @ Python

    原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ 题意: Given a sorted link ...

  10. LeetCode::Remove Duplicates from Sorted List II [具体分析]

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

随机推荐

  1. 依赖注入框架Google Guice 对象图

    GettingStarted · google/guice Wiki https://github.com/google/guice/wiki/GettingStarted sameb edited ...

  2. Python3中json的encode和decode

    在Python3中,将对象序列化为JSON对象,即对对象进行json encode编码,使用函数 json.dumps(obj, *, skipkeys=False, ensure_ascii=Tru ...

  3. Python代码样例列表

    扫描左上角二维码,关注公众账号 数字货币量化投资,回复“1279”,获取以下600个Python经典例子源码 ├─algorithm│       Python用户推荐系统曼哈顿算法实现.py│    ...

  4. type为number的<input>标签 type和size属性失效

    html5中input的type属性增的可取值新增几种,对于不支持这几种新增值的浏览器会统一解析为text类型. Firefox.ie9不支持

  5. 前端构建工具gulpjs的使用介绍及技巧(一)

    原文链接:http://www.cnblogs.com/2050/p/4198792.html gulpjs是一个前端构建工具,与gruntjs相比,gulpjs无需写一大堆繁杂的配置参数,API也非 ...

  6. Notepad++ 更换主题+字体

    Notepad++ 更换主题 https://blog.csdn.net/haluoluo211/article/details/51922666 延伸: 挑选主题 https://blog.csdn ...

  7. python web框架 django工程的创建

    安装 django pip3 install django pip install django 安装完后出现这两个文件 django-admin 用来创建文件夹 在script目录 执行这个命令 d ...

  8. Python-读入json文件并进行解析及json基本操作

      import json def resolveJson(path): file = open(path, "rb") fileJson = json.load(file) fi ...

  9. 015-HQL中级5-hive创建索引

    索引是hive0.7之后才有的功能,创建索引需要评估其合理性,因为创建索引也是要磁盘空间,维护起来也是需要代价的 创建索引 hive> create index [index_studentid ...

  10. Node.js REST 工具 Restify

    Restify 是一个 Node.JS 模块,可以让你创建正确的 REST web services.它借鉴了很多 express 的设计,因为它是 node.js web 应用事实上的标准 API. ...