[leetcode] 13. Remove Duplicates from Sorted List
这个题目其实不难的,主要是我C++的水平太差了,链表那里绊了好久,但是又不像用python,所以还是强行上了。
题目如下:
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given
1->1->2, return1->2.Given
1->1->2->3->3, return1->2->3.
思路很简单,拿到链表后依次遍历,遇到next->val == val;就把next的节点删掉就行。然后就看C++的操作了。
题解如下:
/**
* 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 *aspros = head;
ListNode *deferos = NULL; if (aspros != NULL)
{
while (aspros->next)
{
int nextVal = aspros->next->val;
while (aspros->val != nextVal && aspros->next->next)
{
deferos = aspros;
aspros = aspros->next;
nextVal = aspros->next->val;
}
if (aspros->val == nextVal)
{
if (deferos == NULL)
{
head = aspros->next;
}
else
{
deferos->next = aspros->next; } }
else
{ // 这里是正常链表删完所有重复节点后的出口
return head;
}
aspros = aspros->next;
} // 这里处理只给入一个值的链表的情况
return head;
}
else
{
// 这里处理给入空链表的情况
return head;
}
}
};
[leetcode] 13. Remove Duplicates from Sorted List的更多相关文章
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>
LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...
- [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] 82. Remove Duplicates from Sorted List II_Medium tag: Linked List
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinctnumbe ...
- [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] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)
[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...
- [LeetCode] 83. Remove Duplicates from Sorted List 移除有序链表中的重复项
Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1 ...
- [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项之二
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
随机推荐
- 关于oracle数据库启动报ORA-01122,ORA-01110,ORA-01203错误的解决方法
ORACLE 数据库空间裸设备出问题了,启动oracle失败,解决方法问题现象: 启动ORACLE的时候报如下的错误: Database mounted. ORA-01 ...
- WCF传输大数据 --断点续传(upload、download)
using System; using System.IO; using System.Runtime.Serialization; using System.ServiceModel; namesp ...
- django-auth组件的权限管理
一:自定义权限验证 1.在model中的Meta类自定义权限码 class WorkUser(models.Model): username = models.CharField(u'用户名', ma ...
- [Delphi] 设置线程区域语言防止乱码
uses Windows; 在工程文件中添加一句代码,如下: Application.Initialize; //添加以下一句解决外文系统乱码问题 SetThreadLocale(DWORD(Wor ...
- Node.js 项目打包
Node项目基于Electron打包 Electron打包打包后项目.exe程序包含在文件夹中,基于Electron打包的基础上直接打包成exe程序 参考一 参考二 需求的软件环境: NSIS 2.4 ...
- window.location 属性
属性 含义 值 protocol: 协议 "http:" hostname: 服务器的名字 "b.a.com" port: 端口 "88" ...
- OpenCV学习笔记 - Video Analysis - 录制视频
录制视频 使用自带摄像头录制一段5s的短视频 error & solution fourcc1 = cv2.CV_FOURCC(', 'v') 在实践过程中,运行这一行时报错: 原因分析 在o ...
- 获取select值及判断是否是数字
代码片段 <div class="container-fluid"> <div class="row"> <div class=& ...
- ajax传参里含有特殊字符的坑
问题场景:今天在测试自己手上的页面功能时,发现一个小bug,在用ajax向后台发数据时,只要参数中出现一些特殊字符,控制台会报错http 400的问题,其实就是特殊字符服务器不能解析.好了,问题是找到 ...
- LevelDB Log文件
[LevelDB Log文件] log文件在LevelDb中的主要作用是系统故障恢复时,能够保证不会丢失数据.因为在将记录写入内存的Memtable之前,会先写入Log文件,这样即使系统发生故障,Me ...