【LeetCode练习题】Remove Duplicates from Sorted List II
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,
Given1->2->3->3->4->4->5, return1->2->5.
Given1->1->1->2->3, return2->3.
移除给定链表中重复出现的节点。
代码如下:
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
ListNode dummy();
dummy.next = head;
ListNode *pre = &dummy, *cur = head;
while (cur) {
int i = cur->val;
if (cur->next && cur->next->val == i) {
while (cur && cur->val == i) {
pre->next = cur->next;
delete cur;
cur = pre->next;
}
}
pre = cur;
cur = cur->next;
}
return dummy.next;
}
};
【LeetCode练习题】Remove Duplicates from Sorted List II的更多相关文章
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- [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% ...
- [leetcode] 80. Remove Duplicates from Sorted Array II (Medium)
排序数组去重题,保留重复两个次数以内的元素,不申请新的空间. 解法一: 因为已经排好序,所以出现重复的话只能是连续着,所以利用个变量存储出现次数,借此判断. Runtime: 20 ms, faste ...
- [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 ...
- [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 ...
- [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项之二
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- LeetCode OJ Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- LeetCode 80. Remove Duplicates from Sorted Array II (从有序序列里移除重复项之二)
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
随机推荐
- <php>统计目录数和文件数
$dirn = 0; //目录数 $filen = 0; //文件数 //用来统计一个目录下的文件和目录的个数 function getdirnum($file) { global $dirn; gl ...
- mysql下用户和密码生成管理
应用上线,涉及到用户名和密码管理,随着上线应用的增加,用户名和密码的管理设置成为一个问题.还要对用户赋权,于是想着写一个脚本来管理,看到同事写的一个脚本,满足需求.思路大致是字母替换为数字,账号根据库 ...
- Linux RAR 安装和使用详细说明
描述:Linux默认自带ZIP压缩,最大支持4GB压缩,RAR的压缩比大于4GB. 流程:下载 >安装 > 使用 ----------------------------------- ...
- 由mysql数据库基础上的php程序实现单词的查询、删除、更改和查询
我做了一个php程序,将表单数据添加到数据库,借用mysql扩展库函数实现对mysql数据库的操作,能够实现添加单词.删除单词.更新和查询单词.运行环境是普通的mysql数据库和php.Apache服 ...
- Swift 新语言开发
全书文件夹: 一.Welcome to Swift 二.Language Guide 三.Language Reference /* 译者的废话: 几个小时前熬夜看了WWDC,各种激动,今年非常有料啊 ...
- javasscript学习笔记 之 数组学习二 数组的所有方法
1.push() 和 pop() 栈的方法 后进先出 push() 该方法是向数组末尾添加一个或者多个元素,并返回新的长度. push()方法可以接收任意数量的参数,把它们逐个添加到数组的末尾,并返 ...
- android调试系列--使用ida pro调试so
1.工具介绍 IDA pro: 反汇编神器,可静态分析和动态调试. 模拟机或者真机:运行要调试的程序. 样本:阿里安全挑战赛第二题:http://pan.baidu.com/s/1eS9EXIM 2. ...
- openssl 摘要和签名验证指令dgst使用详解
1.信息摘要和数字签名概述 信息摘要:对数据进行处理,得到一段固定长度的结果,其特点输入: 1.输出长度固定.即输出长度和输入长度无关. 2.不可逆.即由输出数据理论上不能推导出输入数据 4.对输入数 ...
- 转载--DEV GridControl 的一些基本操作
1. 如何解决单击记录整行选中的问题 View->OptionsBehavior->EditorShowMode 设置为:Click 2. 如何新增一条记录 (1).gridView.Ad ...
- 限制内容长度(CSS,jQuery)
CSS(宽度限制在100px之内,超出就会点点点) <style type="text/css"> p{width: 100px;display: inline-blo ...