[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 ...
随机推荐
- Webpack-simple cross-env 不是内部或外部命令问题处理
本文转载自:https://www.cnblogs.com/stono/p/6984222.html Webpack-simple cross-env 不是内部或外部命令问题处理 学习了:https: ...
- 常见的加密和解密算法—AES
一.AES加密概述 高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准.这个标准用 ...
- ZOJ - 3657-The Little Girl who Picks Mushrooms
/*ZOJ Problem Set - 3657 The Little Girl who Picks Mushrooms Time Limit: 2 Seconds Memory Limit: 327 ...
- 《Linux内核精髓:精通Linux内核必会的75个绝技》一HACK #3 如何编写内核模块
HACK #3 如何编写内核模块 本节将介绍向Linux内核中动态添加功能的结构—内核模块的编写方法.内核模块Linux内核是单内核(monolithic kernel),也就是所有的内核功能都集成在 ...
- Introduction to Spring Data MongoDB
Introduction to Spring Data MongoDB I just announced the new Spring 5 modules in REST With Spring: & ...
- 常规Java工具,算法,加密,数据库,面试题,源代码分析,解决方案
原文链接:http://www.tvtvso.top/?thread-5.htm 有家杂志曾对全国60岁以上的老人进行了这样一次问卷调查:你最后悔什么? 列出10项人们生活中容易后悔的事情,供被调查者 ...
- OpenCL 事件的使用,以及回调函数
▶ 事件的两种使用方法.第一种是用事件 a 标记进入命令队列的操作 A,于是后续进入命令队列的操作 B 可以被要求等到前面事件 a 完成(即操作 A 完成)以后才能开始调度执行.第二种是使用用户自定义 ...
- HTTP 协议基础
HTTP 协议的主要特点可概括如下: 1.支持客户/服务器模式. 2.简单快速:客户向服务器请求服务时,只需传送请求方法和路径.请求方法常用的有GET.HEAD.POST.每种方法规定了客户与服务器联 ...
- leetcode111
/** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNo ...
- Linux Tomcat8 访问管理页面 403 Access Denied
http://blog.csdn.net/u012167045/article/details/61624226 1:修改conf/tomcat-users.xml配置文件 2:vi /usr/loc ...