LeetCode OJ--Remove Duplicates from Sorted List II *
http://oj.leetcode.com/problems/remove-duplicates-from-sorted-list-ii/
处理链表的范例
#include <iostream>
using namespace std; struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
}; class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
if(head == NULL) return head; ListNode dummy(INT_MIN);
dummy.next = head;
ListNode *prev = &dummy,*cur = head;
while(cur!= NULL)
{
bool duplicated = false;
while(cur->next != NULL && cur->val == cur->next->val)
{
duplicated = true;
cur = cur->next;
}
if(duplicated)
{
cur = cur->next;
continue;
}
prev->next = cur;
prev = prev->next;
cur = cur->next;
}
prev->next = cur;
return dummy.next;
}
}; int main()
{
ListNode *n1 = new ListNode();
ListNode *n2 = new ListNode();
ListNode *n3 = new ListNode();
ListNode *n4 = new ListNode();
ListNode *n5 = new ListNode();
ListNode *n6 = new ListNode();
ListNode *n7 = new ListNode();
n1->next = n2;
n2->next = n3;
n3->next = n4;
n4->next = n5;
n5->next = n6;
n6->next = n7;
ListNode *ans;
Solution myS;
ans = myS.deleteDuplicates(n1);
return ;
}
LeetCode OJ--Remove Duplicates from Sorted List II *的更多相关文章
- 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 [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 80. Remove Duplicates from Sorted Array II (从有序序列里移除重复项之二)
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
随机推荐
- 题解 P5082 【成绩】
随机跳题跳到了这一题,一看是个红题,本蒟蒻就 艰难地思考起来 高兴地写起来 这题实在不能用数组,用了数组就RE 一开始就卡在这上面了 说实话,这道题真的 很难 不算很难,只要照着公式往上面套就行了 废 ...
- EMVS: Event-based Multi-View Stereo 阅读笔记
0. 摘要 EMVS目的:从已知轨迹的event相机,估计半稠密的3D结构 传统的MVS算法目的:从已知视点的图片集,去估计场景的稠密3D结构. EMVS2个固有属性: (1) 当传感器发生相对运 ...
- C06 变量和存储类型
目录 全局变量 局部变量 存储类型 全局变量和局部变量 变量的作用域 作用域:某些事物起作用或有效的区域. 变量的使用范围称为变量的作用域. 变量的作用域决定了变量的可操作性和有效性. C语言变量的作 ...
- 获取 request 中 json 数据
import java.io.IOException; import javax.servlet.http.HttpServletRequest; /** * request 对象的相关操作 * @a ...
- [OpenJudge] 2727 仙岛寻药
2727:仙岛求药 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 少年李逍遥的婶婶病了,王小虎介绍他去一趟仙灵岛,向仙女姐姐要仙丹救婶婶.叛逆但孝顺的李逍遥闯进 ...
- LeetCode(105) Construct Binary Tree from Preorder and Inorder Traversal
题目 Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume t ...
- 记如何解决蓝桥杯中to_string和atoi等无法使用的问题
#include<iostream> #include<sstream> using namespace std; int main() { // int 转 string ...
- Beyond Compare 30天评估期结束解决办法
打开Beyond Compare 4,提示已经超出30天试用期限制 解决方法: 1.修改文件 修改C:\Program Files\Beyond Compare 4\BCUnrar.dll ,这个文件 ...
- sublime__最全面的 Sublime Text 使用指南
感谢大佬--> 原文链接 摘要(Abstract) 本文系统全面的介绍了Sublime Text,旨在成为最优秀的Sublime Text中文教程. 前言(Prologue) Sublime T ...
- Hive 执行查询语句报错,由于内存空间不足导致
org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.hdfs.server.namenode.SafeModeException): Can ...