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. PHP中的的一个挺好用的函数 array_chunk

  2. F, A, MS, QM, RF的OFFER和经历 -- Final update

    昨天收到FB的电话,我的OFFER已经批下来了,这也意味着我的JOB HUNTING结束了,下 面是我这两个月来申请结果汇总: Applications (7): Facebook, Google, ...

  3. 什么是目标、度量、KPI、维度和细分

    今天看到了Avinash的一篇文章:Web Analytics 101: Definitions: Goals, Metrics, KPIs, Dimensions, Targets,正是我想在影响网 ...

  4. Android开发_关于如何屏蔽Home键

    今天在遇到一个要屏蔽Home键的问题,研究一上午终于解决,方法记录于下: 在Android2.3版本以下重写以下方法就能屏蔽Home键: public void onAttachedToWindow( ...

  5. c# 调用EXCEL在VS上能正常运行,部署在IIS上不能实现,在VS中运行页面和发布之后在IIS中运行的区别

    发现一篇文章,很好,解决了这个问题:感谢原博主!特此做个笔记. 地址:http://www.cnblogs.com/zhongxinWang/p/3275154.html 发布在IIS上的Web程序, ...

  6. CSS混合模式

    前面的话   层叠上下文z-index只是解决两个元素覆盖,谁离用户更近的问题.而CSS混合模式,则是处理两个元素覆盖部分如何混合的问题.如果了解photoshop的话,对这种现象应该不陌生.CSS3 ...

  7. Drawable复习—第六章

    一.Drawable的分类及使用 复习知识:①.Drawable有几种类别. ②.在哪里利用xml创建Drawable  ③.类中各个类别如何使用  ④.Drawable的插值器和设置时常.是否保持动 ...

  8. Joomla JEvents 组件

    JEvents http://extensions.joomla.org/extensions/extension/calendars-a-events/events/jevents Getting ...

  9. python运维开发(九)----socket

    内容目录: socket通信过程 单线程socket 多线程socket ThreadingTCPServer socket socket通常也称作"套接字",用于描述IP地址和端 ...

  10. MYSQL 关于索引的部分问题!

    1. PRIMARY KEY也可以只指定为KEY.这么做的目的是与其它数据库系统兼容.二来key 是index的同意词! 2. 在UNIQUE索引中,所有的值必须互不相同.如果您在添加新行时使用的关键 ...