题目

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

For example,
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.

代码

/**
* 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) {
if ( !head || !head->next ) return head;
ListNode dummy(INT_MIN);
dummy.next = head;
ListNode *prev = &dummy;
ListNode *p = head;
while ( p && p->next )
{
if ( p->val!=p->next->val )
{
prev = p;
p = p->next;
}
else
{
while ( p->next && p->val==p->next->val ) p = p->next;
prev->next = p->next;
p = p->next;
}
}
return dummy.next;
}
};

Tips:

主要思路就是:如果遇上相同的,就用while循环一直往后过。

具体思路沿用了之前Python版的:http://www.cnblogs.com/xbf9xbf/p/4186852.html

====================================================

第二次过这道题,思路忘记了。赶紧翻了了一下上面的笔记,才想起来;代码一次AC了。

/**
* 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) {
if ( !head ) return head;
ListNode dummpy(-);
dummpy.next = head; ListNode* pre = &dummpy;
ListNode* curr = head; while ( curr && curr->next )
{
if ( curr->val!=curr->next->val )
{
pre = curr;
curr = curr->next;
}
else
{
while ( curr->next && curr->val==curr->next->val )
{
curr = curr->next;
}
pre->next = curr->next;
curr = curr->next;
}
}
return dummpy.next;
}
};

这里的思路关键点是while循环的判断条件(curr && curr->next)。

【Remove Duplicates from Sorted List II 】cpp的更多相关文章

  1. 【Remove Duplicates from Sorted Array II】cpp

    题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...

  2. leetcode 【 Remove Duplicates from Sorted Array II 】python 实现

    题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...

  3. leetcode 【 Remove Duplicates from Sorted List II 】 python 实现

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

  4. 【Search in Rotated Sorted Array II 】cpp

    题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would t ...

  5. 82. Remove Duplicates from Sorted List II【Medium】

    82. Remove Duplicates from Sorted List II[Medium] Given a sorted linked list, delete all nodes that ...

  6. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  7. 【LeetCode练习题】Remove Duplicates from Sorted List II

    Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...

  8. 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  9. Remove Duplicates from Sorted List II

    Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...

随机推荐

  1. npm常用指令小记

    查看本地指定包在npm远程服务器的版本信息 方式一: npm view <packageName> versions 方式二: npm info <packageName> 查 ...

  2. Centos7_Minimal-1611 版安装python3.5.3

    前提 最近在学习python3,看到好多教程都是要求在Windows或者Ubuntu 平台上使用,安装比较方便.由于不在想Winddows上安装也没有Ubutnu系统 ,所以在自己的CentOS7上面 ...

  3. int _tmain(int argc, _TCHAR* argv[])

    int _tmain(int argc, _TCHAR* argv[]){ int i; for (i = 0; i<argc; i++) cout<<argv[i]<< ...

  4. pat乙级1067

    1.用cin输入数据后,再用getline 输入,还是会输入cin已经输入的数据,即cin和getline互相独立. 2.题目中没有说尝试的密码不包含空格,因此不能用cin,而用getline. #i ...

  5. POJ-1459 Power Network---最大流

    题目链接: https://cn.vjudge.net/problem/POJ-1459 题目大意: 简单的说下题意(按输入输出来讲,前面的描述一堆的rubbish,还用来误导人),给你n个点,其中有 ...

  6. 【BZOJ1257】[CQOI2007] 余数之和(数学题)

    点此看题面 大致题意: 求\(\sum_{i=1}^nk\%i\). 关于除法分块 这是一道除法分块的简单应用题. 式子转换 显然\(k\%i\)是一个很难处理的项. 于是我们就要使用使用一个常用的套 ...

  7. python_13_break

    for i in range(5): print('-----------',i) for j in range(5): print(j) if j>2: break####结束当前循环

  8. 记Tea使用中遇到的问题及解决过程

    学习Markdown时,在小众软件看到一个叫Tea的软件.UI设计是简约风格:"所见即所得"的Markdown:支持插件等原因让我选择去尝试这杯"茶". 最近一 ...

  9. 简述apache,php,mysql三者的关系

    转自:http://blog.csdn.net/w1365966490/article/details/8218959 Apache web 服务器软件.同类产品有微软的 IIS 等.功能是让某台电脑 ...

  10. JT∕T 905 -2014 出租汽车服务管理信息系统的相关协议研究

    出租汽车服务管理信息系统(JT∕T 905 -2014) 国家的相关技术要求2014年7月正式出台,总体有四部分,   第 1 部分:总体技术要求:   第 2 部分:运营专用设备:   第 3 部分 ...