题目

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. linux性能测试脚本

    http://linux-bench.com/ What is Linux-Bench? Linux-Bench is a simple script that provides a basic le ...

  2. PHP的模板引擎smarty原理浅谈

    mvc是开发中的一个伟大的思想,使得开发代码有了更加清晰的层次,让代码分为了三层各施其职.无论是对代码的编写以及后期的阅读和维护,都提供了很大的便利. 我们在php开发中,视图层view是不允许有ph ...

  3. 如何在InstallShield的MSI工程中调用Merge Module的Custom Action

    使用InstallShield创建了合并模块安装程序,定义自定义活动,可如何调用却不太清楚,网上也就找到这点信息,还是没有成功,到底该在什么地方执行合并模块的自定义活动? http://1662487 ...

  4. Spark的调度

    作业调度简介 设计者将资源进行不同粒度的抽象建模,然后将资源统一放入调度器,通过一定的算法进行调度,最终要达到高吞吐或者低访问延时的目的. Spark在各种运行模式中各个角色实现的功能基本一致,只不过 ...

  5. Unix系统中常用的信号含义

    http://blog.csdn.net/u012349696/article/details/50687462 编号为1 ~ 31的信号为传统UNIX支持的信号,是不可靠信号(非实时的),编号为32 ...

  6. iOS开发遇到的坑之七--上传app Stroe被拒绝:The app references non-public symbols in : _UICreateCGImageFromIOSurface

    这是上学期遇到的问题了,突然查阅邮箱的时候发现了,遂在这里记录下来,希望大家以后注意这个问题 我上传App Store的时候,apple给我发的邮件原文: Dear developer, We hav ...

  7. iOS 中push和pop到底系统做了些什么事

    iOS中的push和pop是一个很常用的视图切换方法,他们是成对出现的, 简而言之,push就是压栈,pop就是出栈! [self.navigationController pushViewContr ...

  8. utf8、ansii、unicode编码之间的转换

    #include "stdafx.h"#include "windows.h"#include <iostream>#include <str ...

  9. 51nod——2478 小b接水(预处理 思维)

    我本来想把每个谷都处理了,想了下觉得不好办.后来看其他人写的是处理每个位置,把每个位置可以接的水累加起来.整挺好. #include <bits/stdc++.h> using names ...

  10. Pots POJ - 3414 (搜索+记录路径)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22688   Accepted: 9626   Special J ...