这个题目其实不难的,主要是我C++的水平太差了,链表那里绊了好久,但是又不像用python,所以还是强行上了。

题目如下:

Given a sorted linked list, delete all duplicates such that each element appear only once.

For example,

Given 1->1->2, return 1->2.

Given 1->1->2->3->3, return 1->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的更多相关文章

  1. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

  2. 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++> 给出排序好的 ...

  3. [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% ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)

    [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...

  8. [LeetCode] 83. Remove Duplicates from Sorted List 移除有序链表中的重复项

    Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1 ...

  9. [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项之二

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

随机推荐

  1. Navicat Premium解决连接mssql报错的问题

    连接名:mssql_172.16.30.21:1433,每次打开查询时就报错. 重启,重转都不好使. 解决办法:去掉“:1433”,因为文件目录不支持“:”,所以一直报错.问题终于得到解决.

  2. Fragment 横竖屏切换问题

    转自:http://my.oschina.net/u/614511/blog/76444 在默认情况下当发生横竖屏切换时,当前Activity中的fragment都会通过Fragment.instan ...

  3. Java 简单UDP通信

    Java手册 java.net Class DatagramSocket java.lang.Object java.net.DatagramSocket All Implemented Interf ...

  4. [Nlog]使用经验

    <?xml version="1.0" ?> <nlog xmlns="http://www.nlog-project.org/schemas/NLog ...

  5. Calendar时间操作

    1.Calendar是Java提供的一个util类,在java.util中.我们在开发过程中经常遇到,需要对时间进行处理的方式.而Java中Calendar给我们提供了很好的控制方式. 2.我这里主要 ...

  6. c++builder 字节 编码 转换大全 String TBytes byte

    System.SysUtils System::DynamicArray<System::WideChar> TCharArray System::TArray__1<System: ...

  7. 【324】Python 库说明(安装&卸载)

    参考:Python_安装官方whl包和tar.gz包 参考:Unofficial Windows Binaries for Python Extension Packages 参考:PyPI 参考:直 ...

  8. ADT下载地址整理(转)

    转自: http://blog.csdn.net/xqf222/article/details/9821971 参考下面文章 http://developer.android.com/tools/sd ...

  9. 一些jquery常用方法

    1.jquery实现平滑滚动到指定锚点 $(document).ready(function() { $("a.topLink").click(function() { $(&qu ...

  10. 二分图 and code1170 双栈排序

    6.6二分图 二分图是这样一个图: 有两顶点集且图中每条边的的两个顶点分别位于两个顶点集中,每个顶点集中没有边直接相连接. 无向图G为二分图的充分必要条件是,G至少有两个顶点,且其所有回路的长度均为偶 ...