1. Remove Duplicates from Sorted List

  题目链接

  题目要求:

  Given a sorted linked list, delete all duplicates such that each element appear only once.

  For example,
  Given 1->1->2, return 1->2.
  Given 1->1->2->3->3, return 1->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; int preVal = head->val;
ListNode *preNode = head;
ListNode *start = head->next;
while(start)
{
while(start && start->val == preNode->val)
{
ListNode *next = start->next;
preNode->next = next;
delete start;
start = next;
} if(!start)
break; preNode = start;
start = start->next;
} return head;
}
};

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

  该题可以通过添加dummy节点以方便编程。具体程序如下:

 /**
* 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; int preVal = head->val;
ListNode *dummy = new ListNode();
dummy->next = head;
ListNode *prepreNode = dummy, *preNode = head;
ListNode *start = head->next;
while(start)
{
bool flag = true;
while(start && start->val == preNode->val)
{
flag = false;
ListNode *next = start->next;
preNode->next = next;
delete start;
start = next;
} if(flag)
{
prepreNode = preNode;
preNode = preNode->next;
}
else
{
prepreNode->next = preNode->next;
delete preNode;
preNode = prepreNode->next;
} if(!preNode || !preNode->next)
break;
start = preNode->next;
} head = dummy->next;
delete dummy;
dummy = nullptr; return head;
}
};

LeetCode之“链表”:Remove Duplicates from Sorted List && Remove Duplicates from Sorted List II的更多相关文章

  1. Leetcode 题目整理-6 Swap Nodes in Pairs & Remove Duplicates from Sorted Array

    24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...

  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. Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II

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

  4. LeetCode 单链表专题 (一)

    目录 LeetCode 单链表专题 <c++> \([2]\) Add Two Numbers \([92]\) Reverse Linked List II \([86]\) Parti ...

  5. leetcode 单链表相关题目汇总

      leetcode-19-Remove Nth From End of List—移除链表中倒数第n个元素 leetcode-21-Merge Two Sorted Lists—两个已排序链表归并 ...

  6. Leetcode解题-链表(2.2.0)基础类

    1 基类的作用 在开始练习LeetCode链表部分的习题之前,首先创建好一个Solution基类,其作用就是: Ø  规定好每个子Solution都要实现纯虚函数test做测试: Ø  提供了List ...

  7. 乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array

    乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array 一.前言 这次我们还是要改造二分搜索,但是想法却 ...

  8. 【算法题 14 LeetCode 147 链表的插入排序】

    算法题 14 LeetCode 147 链表的插入排序: 解题代码: # Definition for singly-linked list. # class ListNode(object): # ...

  9. C#LeetCode刷题之#83-删除排序链表中的重复元素(Remove Duplicates from Sorted List)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3820 访问. 给定一个排序链表,删除所有重复的元素,使得每个元素 ...

随机推荐

  1. 细说Http协议

    什么Http协议 HTTP是HyperText Transfer Protocol(超文本传输协议)的简写,它是TCP/IP协议的一个应用层协议,用于定义WEB浏览器与WEB服务器之间交换数据的过程及 ...

  2. Android图表库MPAndroidChart(五)——自定义MarkerView实现选中高亮

    Android图表库MPAndroidChart(五)--自定义MarkerView实现选中高亮 在学习本课程之前我建议先把我之前的博客看完,这样对整体的流程有一个大致的了解 Android图表库MP ...

  3. Android系统的安全设计与架构

    Android系统的安全设计与架构 一.安全策略 1.Android 的总体架构由5个主要层次上的组件构成,这5层是:Android应用层. Android框架层.Dalvik虚拟机层.用户空间原生代 ...

  4. 使用Apache的ab进行压力测试

    概述 ab是apache自带的压力测试工具,当安装完apache的时候,就可以在bin下面找到ab然后进行apache 负载压力测试. 后台测试开发中,常用的压力测试服务,php一般选择xampp,下 ...

  5. 全文检索 Lucene(3)

    看完前两篇博客之后,想必大家对于Lucene的使用都有了一个比较清晰的认识了.如果对Lucene的知识点还是有点模糊的话,个人建议还是先看看这两篇文章. 全文检索 Lucene(1) 全文检索 Luc ...

  6. [ExtJS5学习笔记]第十二节 Extjs5开发遇到的问题列表记录

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/38975633 本文作者:sushengmiyan ------------------ ...

  7. 1070. Mooncake (25)

    题目如下: Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many ...

  8. (九十六)借助APNS实现远程通知、后台任务

    APNS全称为Apple Push Notification Service,可以实现在app不启动时也能通过服务器推送到iOS端特定设备的功能. APNS的实现原理为先发送设备的UDID和应用的Bu ...

  9. python进行md5加密

    代码函数 import hashlib def md5(str): m = hashlib.md5() m.update(str) return m.hexdigest() f = open('idf ...

  10. Swift基础之闭包Closure学习

    首先Swift语言中没有了Block内容,但是你可以通过调用OC文件使用,也可以使用Closure(闭包),实现Block或者Delegae同样反向传值或回调函数的效果,也可以解决函数指针的问题,两者 ...