Remove Duplicates from Sorted List II

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.

分析: 为了便于管理头指针,创建一个新的头指针,利用几个新的变量来判断cur和prev的关系

/**
* 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 ==NULL)
return head; ListNode* start = new ListNode();
start->next = head; ListNode* cur = head;
ListNode* prev = start;
while(cur){
while(cur->next && cur->next->val == prev->next->val)
cur = cur->next;
if(prev->next == cur)
prev = cur;
else
prev->next = cur->next;
cur = cur->next;
}
return start->next;
}
};

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

  1. 【leetcode】Remove Duplicates from Sorted Array II

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

  2. 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element

    Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...

  3. 48. Remove Duplicates from Sorted List && Remove Duplicates from Sorted List II

    Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such that each ...

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

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

  5. Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II

    以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置 一:Remove Element Given an array and a value, remove a ...

  6. LeetCode之“链表”:Remove Duplicates from Sorted List && Remove Duplicates from Sorted List II

    1. Remove Duplicates from Sorted List 题目链接 题目要求: Given a sorted linked list, delete all duplicates s ...

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

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

  8. leetcode 203. Remove Linked List Elements 、83. Remove Duplicates from Sorted List 、82. Remove Duplicates from Sorted List II(剑指offer57 删除链表中重复的结点)

    203题是在链表中删除一个固定的值,83题是在链表中删除重复的数值,但要保留一个:82也是删除重复的数值,但重复的都删除,不保留. 比如[1.2.2.3],83题要求的结果是[1.2.3],82题要求 ...

  9. Leetcode: Remove Duplicates from Sorted List II 解题报告

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

随机推荐

  1. 利用CSS3中transparent实现三角形及三角形组合图

    ??如何绘制三角形及三角形的组合图案,以下是自己画的草图 源码如下 <!DOCTYPE html> <html lang="en"> <head> ...

  2. getElementsByTagName() 方法

    HTML DOM Document 对象 定义和用法 getElementsByTagName() 方法可返回带有指定标签名的对象的集合. 语法 document.getElementsByTagNa ...

  3. 关于arcgis engine的注记显示与关闭问题

    1.注记的添加需要拿到IGeoFeatureLayer接口下的AnnotationProperties属性,转为IAnnotationLayerPropertiesCollection接口,并创建一个 ...

  4. Linux安全基础:find命令的使用

    find 命令用于查找文件系统中的指定文件. *命令格式:find pathname -option [-print -exex -ok] 1.pathname要查找的目录路径 ~表示home目录 . ...

  5. git笔记

    这篇有关git的博客,写着写着有些崩了.里面有些碎碎念了.下次一定注意这个问题. 创建项目: midir xx :创建xx文件夹 git init : 为当前文件夹创建代码仓库 提交代码: git a ...

  6. MySQL的数据库与表格创建

    打开MySQL: 1.进入终端输入:cd /usr/local/mysql/bin/ 2.回车后 登录管理员权限 sudo su 3.回车后输入以下命令来禁止mysql验证功能 ./mysqld_sa ...

  7. 在执行xp_cmdshell的过程中出错,调用'LogonUserW'失败,错误代码:'1909'

    在上篇文章Could not obtain information about Windows NT group/user 'xxxx\xxxx', error code 0x5里面,我介绍了SQL ...

  8. SQL Server Column Store Indeses

    SQL Server Column Store Indeses SQL Server Column Store Indeses 1. 概述 2. 索引存储 2.1 列式索引存储 2.2 数据编码和压缩 ...

  9. java jdbc url 不同数据库

    1.Oracle数据库Class.forName("oracle.jdbc.driver.OracleDriver" ).newInstance() ;String url = & ...

  10. mysql 5.5.32 多实例环境的启动问题

      [root@localhost scripts]# /bin/sh mysql_install_db --user=mysql --dasedir=/application/mysql --dat ...