Total Accepted: 59433 Total Submissions: 230628 Difficulty: Medium

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.

class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
ListNode* help_node = new ListNode();
help_node->next = head; ListNode* cur_pre = help_node;
ListNode* cur = head; while(cur!=NULL){
ListNode* p = cur->next;
while(p!=NULL && p->val==cur->val){
p = p->next;
} if(p != cur->next){
while(cur != p) {
ListNode* tmp = cur;
cur = cur->next;
delete (tmp);
} cur_pre->next = p;
cur = p;
}else{
cur_pre = cur;
cur = cur->next;
}
} head = help_node->next;
delete (help_node);
return head;
}
};

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

  1. LeetCode[Linked List]: Remove Duplicates from Sorted List II

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

  2. 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题要求 ...

  3. Remove Duplicates from Sorted List II

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

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

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

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

  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: Remove Duplicates from Sorted List II 解题报告

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

  8. 82. Remove Duplicates from Sorted List II && i

    题目 83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such tha ...

  9. 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 ...

随机推荐

  1. Deppon接口开发

    一.1)  支持的传输协议  http ,暂时只支持HTTP协议进行通信. (2) 支持的数据传输格式  Json  ,所有接口暂只支持json消息格式. (3) 编码格式:UTF-8   交互编码格 ...

  2. photoswipe

    allowUserZoom: 允许用户双击放大/移动方式查看图片. 默认值 = trueautoStartSlideshow: 当PhotoSwipe激活后,自动播放幻灯片. 默认值 = falsea ...

  3. 将json的时间格式转换成正常的时间格式

    /** * 对Date的扩展,将 Date 转化为指定格式的String * 月(M).日(d).12小时(h).24小时(H).分(m).秒(s).周(E).季度(q) 可以用 1-2 个占位符 * ...

  4. 异常 java.lang.NumberFormatException: For input string:

    今天在写项目时,将String类型转换为Integer类型爆出此异常,记录如下: 代码如下: 1 String a = "2222222222"; //10个2 Integer b ...

  5. ubuntu安装体验

    本文记录一下昨晚及今天安装ubuntu系统的过程及体验 2016年6月13日09:36:11 更新 今天才有发现原来自己有个没填的坑 = = 那次安乌班图后第一感觉是很好用,新鲜了好几天,但是很快,新 ...

  6. node.js入门(二) 第一个程序 Hello World

    新建一个名为"hello.js"文本文件,然后输入如下内容 //载入http模块 var http = require('http'); //构建一个http服务器 var ser ...

  7. c语言-函数的定义及传参

    模块化编程 定义: 数据类型 函数名(型参):如果没有数据类型,默认的为int 值传递 地址传递 嵌套调用 递归调用:直接或间接调用本身函数,求可能栈破裂,求阶乘 #include <stdio ...

  8. HTML5 canvas 在线画笔绘图工具(三)

    组装画板(TDrawBuilder) 在这一小节中我们要把工具条和画板组装起来,让他们可以协同进行工作. 画板通过一个命名为TDrawBuilder来进行组装.在详细讲解TDrawBuilder对象之 ...

  9. 踩坑学php(1)

    前言: 为什么要学php 呢?作为一个前端,一直有着了解后台的好奇心:作为一个计算机毕业的,一直有着实践更多设计模式和数据库相关的东西:而php 非常流行,拥有非常多的资源,入门应该容易: 为什么叫& ...

  10. Computer Transformation(hdoj 1041)

    Problem Description A sequence consisting of one digit, the number 1 is initially written into a com ...