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. [转载]Elasticsearch、MongoDB和Hadoop比较

    IT界在过去几年中出现了一个有趣的现象.很多新的技术出现并立即拥抱了“大数据”.稍微老一点的技术也会将大数据添进自己的特性,避免落大部队太远,我们看到了不同技术之间的边际的模糊化.假如你有诸如Elas ...

  2. linux 命令行 执行 php

    w为监控响应功能做准备. ubuntu@VM-52-248-ubuntu:~$ php -f /var/www/html/wlinux.phpwwubuntu@VM-52-248-ubuntu:~$ ...

  3. window 如何枚举设备并禁用该设备和启用该设备?如何注册设备热拔插消息通知?

    目前实现的功能: 1.设备枚举 2.设置设备禁用和启用 3.注册设备热拔插消息通知 4.获取设备 vid pid 数值 需要链接的库 SetupAPI.lib DeviceManager 类如下: D ...

  4. Linux下的信号机制

    2017-04-06 之前在看LinuxThreads线程模型的时候,看到该模型是通过信号实现线程间的同步,当时没有多想,直接当做信号量了,现在想起来真是汗颜……后来想想并不是那么回事,于是,就有了今 ...

  5. WCF服务对于处理客户端连接的一点思考

    对于每个客户端的,服务端是否为每个客户端有专门的“通道”? 目的:想在服务端记录下来客户端的访问记录(进入.各个操作.离开等信息),并将其执行的操作独立记录在各个客户端对应的日志中. 下面是代码: 契 ...

  6. 通过进程名称删除进程 ps -ef

    删除进程名为udpserver的进程. kill -9 $(ps -ef|grep udpserver | grep java|awk '{print $2}' ). 1.通过进程名取得进程号: ps ...

  7. PAT 1096 Consecutive Factors[难]

    1096 Consecutive Factors (20 分) Among all the factors of a positive integer N, there may exist sever ...

  8. 闪回事务(Flashback Transaction)

    到目前为止,介绍的所有功能均不会直接将数据恢复为“以前”的样子.闪回查询只是查看,闪回数据归档只是延伸了闪回查询的时间窗口,闪回事务查询虽然提供了撤销SQL,但是否执行及如何执行还需要管理员进一步手动 ...

  9. 如何使用别人的代码 (特指在MFC里面 或者推广为C++里面)

    别人写了一堆代码,给了你源代码.在C++里面 应该是  头文件(.h)和源文件(.cpp).  那么我们如何使用他们呢?? 第一步:将其包含进来 如下图  ,不论是头文件还是源文件都如此 第二步:告诉 ...

  10. Part01、sqlalchemy 使用

    一.ORM         连表               一对多               1.创建表,主动指定外键约束.               2.操作.                 ...