[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 ...
随机推荐
- Java 判断是否包含指定的子串 contains()
Java 手册 contains public boolean contains(CharSequence s) 当且仅当此字符串包含指定的 char 值序列时,返回 true. 参数: s - 要搜 ...
- Python基础之文件
输出一行一行的,效率更高 一个任务: 主函数:
- Bootstrap-Plugin:附加导航(Affix)插件
ylbtech-Bootstrap-Plugin:附加导航(Affix)插件 1.返回顶部 1. Bootstrap 附加导航(Affix)插件 附加导航(Affix)插件允许某个 <div&g ...
- 集群(heartbeat)搭建
HA 即(high available cluster)高可用集群,又称双机热备,保证关键性业务的不间断提供服务. 如:两台机器A和B,正常情况A提供服务,B待命闲置:一但A宕机或服务宕掉,自动切换至 ...
- 使用pandas时遇到ValueError: numpy.dtype has the wrong size, try recompiling
[问题]使用pandas时遇到ValueError: numpy.dtype has the wrong size, try recompiling [原因] 这是因为 Python 包的版本问题,例 ...
- webdriver定位页面元素时使用set_page_load_time()和JavaScript停止页面加载
webdriver定位页面元素时使用set_page_load_time()和JavaScript停止页面加载 原文:https://my.oschina.net/u/2344787/blog/400 ...
- selenium webdriver——JS操作日历控件
一般的日期控件都是input标签下弹出来的,如果使用webdriver 去设置日期, 1. 定位到该input 2. 使用sendKeys 方法 比如 但是,有的日期控件是readonly的 比如12 ...
- leetcode110
/** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNo ...
- Maven(二)了解Maven仓库
转载自: http://blog.csdn.net/gebitan505/article/details/46233981 在使用 Maven 之前,我们必须要了解一下 Maven 到底是怎样管理 j ...
- onItemLongClick+onCreateContextMenu实现长按ListItem弹出不同菜单
个ListActivity,长按不同的item弹出的菜单不一样 参照