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. 剑指Offer——“你最大的缺点是什么”回答技巧及范例

    剑指Offer--"你最大的缺点是什么"回答技巧及范例   问题分析:认识自己的缺点是一个巨大的优点, 当HR问到你缺点的时候, 你的机会来了, 请快展示你的自知之明吧!你想把优点 ...

  2. Lua判断OS并添加cpath

    Lua判断OS并添加cpath(金庆的专栏)Lua初始化时需要根据OS来设置package.cpath, 如果是Windows系统则添加 ?.dll, 否则添加 ?.so.不然加载错误后缀名的动态库会 ...

  3. 海量并发的无锁编程 (lock free programming)

    最近在做在线架构的实现,在线架构和离线架构近线架构最大的区别是服务质量(SLA,Service Level Agreement,SLA 99.99代表10K的请求最多一次失败或者超时)和延时.而离线架 ...

  4. JRE System Library [JavaSE-1.7](unbound)

    window > preferences > java > Install jars >如果没有jdk1.7 ,点击下面的search,会自动找到已经安装对jdk1.7,选择, ...

  5. Shell脚本编程入门(一)

    最近在学shell,记录一下. if语句的使用: 1.判断两个参数大小 #!/bin/sh #a test about if statement a=10 b=20 if [ $a -eq $b ]; ...

  6. open_links_per_instance 和 open_links 参数说明

    1.1  OPEN_LINKS Property Description Parameter type Integer Default value 4 Modifiable No --即修改需要重启实 ...

  7. Android的ANR详解(原因和方案)

    ANR的定义 在Android上,如果你的应用程序有一段时间响应不够灵敏,系统会向用户显示一个对话框,这个对话框称作应用程序无响应(ANR:Application Not Responding)对话框 ...

  8. 深入浅出Java Dom4j读取XML

    在以前自己使用的xml较少,只是了解其很强大,现在可算是在DRP中,真正的开始使用它了,以前只是简单的理解xml,xml即可扩展标记语言,简单的使用,具体是什么?怎么用?还是一直让自己期待的. 首先来 ...

  9. XStream

     1.引入需要的jar包,在pom.xml中配置依赖 <dependency> <groupId>com.thoughtworks.xstream</groupId& ...

  10. python 内存数据库与远程服务

    python 内存数据库与远程服务 需要import的python 内存数据库代码参考下面的链接: http://blog.csdn.net/ubuntu64fan/article/details/5 ...