【Leetcode】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) {
if (head == nullptr || head->next == nullptr)
return head;
ListNode dummy(-);
dummy.next = head;
ListNode *prev = &dummy, *cur = head;
while (cur != nullptr) {
bool flag = false;
while (cur->next != nullptr && cur->val == cur->next->val) {
flag = true;
prev->next = cur->next;
delete cur;
cur = prev->next;
}
if (flag) {
prev->next = cur->next;
delete cur;
cur = prev->next;
} else {
prev = cur;
cur = cur->next;
}
}
return dummy.next;
}
};
【Leetcode】Remove Duplicates from Sorted List II的更多相关文章
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 【leetcode】Remove Duplicates from Sorted List II (middle)
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- 【leetcode】Remove Duplicates from Sorted Array I & II(middle)
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- 【Leetcode】【Medium】Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- 【数组】Remove Duplicates from Sorted Array II
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- 【leetcode】Remove Duplicates from Sorted Array
题目描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ...
- 【leetcode】Remove Duplicates from Sorted List
题目简述 Given a sorted linked list, delete all duplicates such that each element appear only once. For ...
- 【leetcode】 Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- 【leetcode】Remove Duplicates from Sorted List (easy)
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
随机推荐
- DDD学习笔录——领域驱动设计DDD概念总结
- Shell编程进阶 1.8 for循环
产生序列的命令 seq 1 2 3 4 5 6 7 8 9 10 seq 1 3 5 7 9 (从1开始增加2显示这个数字,到10结束) seq - 10 8 6 4 2 seq - 10 9 8 ...
- 链接ssh失败问题
Starting sshd:/var/empty/sshd must be owned by root and not group or world-writable. ...
- DAY19-Pillow制作验证码
PIL:Python Imaging Library,已经是Python平台事实上的图像处理标准库了.PIL功能非常强大,但API却非常简单易用. 由于PIL仅支持到Python 2.7,加上年久失修 ...
- Android Studio Build APK没有报错,但是Generate signed apk报错
有时候 ,我们在调试APK,直接Build是可以正常生成,没有报错,但是当我们将自己的签名文件加上去,就会报错.一般情况下,我们可以在build.gradle中的android{}里面添加一个东西 l ...
- Xcode快捷
- madplay的使用方法
管理madplay的主程序,包括播放,暂停播放,恢复播放,停止播放 system("madplay north.mp3 &");//利用system函数调用madplay播 ...
- Enumeration与Iterator的对比
Enumeration与Iterator的对比 Enumeration 接口 Iterator 接口 参数的含义 枚举类型 迭代器元素类型 所在包 java.util 父类 无 子类 StringTo ...
- Android 自定义格式的对话框
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAaoAAADvCAIAAAAsDwfKAAAgAElEQVR4nLy9bZhcVZUofEg0GcURBk ...
- maven安装第三方jar包到本地仓库
添加项目依赖的时候,有些jar下载不下来,只有手动下载或安装到本地仓库了 首先下载所需要的jar,放到指定的文件夹 然后执行如下命令: mvn install:install-file -Dfile= ...