[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 ...
随机推荐
- 学习OCI编程
转自:http://kulong0105.blog.163.com/blog/static/174406191201162145944574/ 最近公司做的一个项目,要处理海量数据,数据是存放在Ora ...
- VS编译后事件
拷贝文件 copy /y "$(OutDir)Maticsoft.BuilderDALParam.dll" "D:\Project Area\2016-3-28" ...
- generatorConfig.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration ...
- gSOAP:C++编写服务器端
1.编写头文件cal.h: //gsoap ns service name: calc //gsoap ns service style: rpc //gsoap ns service encodin ...
- 给iOS开发新手送点福利,简述UITextField的属性和用法
UITextField属性 0. enablesReturnKeyAutomatically 默认为No,如果设置为Yes,文本框中没有输入任何字符的话,右下角的返回按钮是disabled的. ...
- maven install 跳过测试
mvn命令跳过测试:mvn install -Dmaven.test.skip=true 测试类不会生成.class 文件mvn install -DskipTests 测试类会生成.class文件 ...
- @JsonIgnore的源码说明
@JsonIgnore不仅仅是在getter上有影响,也对setter方法有影响: 所在包:com.fasterxml.jackson.annotation; 源码: import java.lang ...
- 【python 】装饰器 (多个参数的函数,带参数的装饰器)【转】
最简单的模板是这样的 #-*-coding:utf-8-*- def outer(func): def inner(): print 'before' func() print 'after' # r ...
- Maven(九)”编码 gbk 的不可映射字符“ 问题解决方案
解决这个问题的思路: 在maven的编译插件中声明正确的字符集编码编码——编译使用的字符集编码与代码文件使用的字符集编码一致!! 安装系统之后,一般中文系统默认字符集是GBK.我们安装的软件一般都继承 ...
- python's mutable & immutable
[python's mutable & immutable] python里面的类型其实也分为immutable和mutable二种,对于mutable,如果b指向a,当b改变时,a也会改变: ...