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. Oracle数据类型与.NET中的对应关系(转)

    Oracle数据类型与.NET中的对应关系 2011-02-24 10:02:16 标签:C# oracletype Oracle 数据类型 .NET Oracle连接添加的引用不同,会存在数据类型不 ...

  2. JSP 实现 之 读取数据库显示图片

    用JSP从数据库中读取图片并显示在网页上 环境mysql+tomcat: <1>先在mysql下建立如下的table. 并insert图像. mysql.sql文件如下: ) unsign ...

  3. HDU 5794 - A Simple Chess

    HDU 5794 - A Simple Chess题意: 马(象棋)初始位置在(1,1), 现在要走到(n,m), 问有几种走法 棋盘上有r个障碍物, 该位置不能走, 并规定只能走右下方 数据范围: ...

  4. stl的集合set——安迪的第一个字典(摘)

    set就是数学上的集合——每个元素最多只出现一次,和sort一样,自定义类型也可以构造set,但同样必须定义“小于”运算符 以下代码测试set中无重复元素 #include<iostream&g ...

  5. A - 487-3279

    Description Businesses like to have memorable telephone numbers. One way to make a telephone number ...

  6. 动态链接库的生成(dll)和 动态链接库隐式and显式调用

    一.构建动态链接库(dll.dll dll.lib dll.h) 说明: .dll 是在执行程序是调用  .lib 是在连接程序是调用  .h是在编译程序时调用 1.头文件(声明导入函数):_decl ...

  7. asp.net 获取当前url地址

    设当前页完整地址是:http://www.jb51.net/aaa/bbb.aspx?id=5&name=kelli "http://"是协议名 "www.jb5 ...

  8. Sum Problem-hdu-1001

    Hey, welcome to HDOJ(Hangzhou Dianzi University Online Judge). In this problem, your task is to calc ...

  9. shell基础——字符串处理(转载)

    Shell的字符串处理   1 得到长度   %x="abcd"  #方法一      %expr length $x      4  # 方法二      %echo ${#x} ...

  10. QT:给Widget设置背景图片——设置Widget的调色板,调色板使用图片和背景色

    QT:给Widget设置背景图片 1 /*2 * set background image3 */4 QPixmap bgImages(":/images/bg.png");5 Q ...