描述

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) {
ListNode* tmp = head;
while(tmp){
while(tmp -> next && tmp -> val == tmp -> next -> val)
tmp -> next = tmp -> next -> next;
tmp = tmp -> next; }
return head;
}
};

另一解法如下:

https://discuss.leetcode.com/topic/33179/recommend-for-beginners-c-implementation-with-detailed-explaination

class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(head == NULL) return NULL;
ListNode* pre = head,*cur = head -> next;
while(cur){
if(pre -> val == cur -> val){
pre -> next = cur -> next;
cur = cur -> next;
}else{
pre -> val = cur -> val;
pre = pre -> next;
cur = cur -> next;
}
}
};

leetcode解题报告(6):Remove Duplicates from Sorted List的更多相关文章

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

  2. <LeetCode OJ> 83. Remove Duplicates from Sorted List

    83. Remove Duplicates from Sorted List Total Accepted: 94387 Total Submissions: 264227 Difficulty: E ...

  3. leetCode练题——26. Remove Duplicates from Sorted Array

    1.题目 26. Remove Duplicates from Sorted Array--Easy Given a sorted array nums, remove the duplicates  ...

  4. LeetCode(80)Remove Duplicates from Sorted Array II

    题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  5. 【LeetCode算法-26】Remove Duplicates from Sorted Array

    LeetCode第26题 Given a sorted array nums, remove the duplicates in-place such that each element appear ...

  6. leetcode第26题--Remove Duplicates from Sorted Array

    problem: Given a sorted array, remove the duplicates in place such that each element appear only onc ...

  7. LeetCode(28)-Remove Duplicates from Sorted Array

    题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  8. LeetCode记录之26——Remove Duplicates from Sorted Array

    国外的表达思维跟咱们有很大差别,做这道题的时候很明显.简单说本题就是让你把有序数组中的重复项给换成正常有序的.比如 1 2 2 3换成 1 2 3 3,根本不需要考虑重复的怎么办,怎么删除重复项等等. ...

  9. LeetCode(82)Remove Duplicates from Sorted List

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

  10. LeetCode(26) Remove Duplicates from Sorted Array

    题目 Given a sorted array, remove the duplicates in place such that each element appear only once and ...

随机推荐

  1. 在一个form表单中实现多个submit不同的action

    在button中用JS的事件绑定onclick实现,如下: <!-- employees是表单的name属性值--> <script type="text/javascri ...

  2. Java内存 模型理解

    概述 在正式讲Java内存模型之前,我们先了解一些物理计算机并发问题,然后一点点的引出Java内存模型的由来. 多任务处理在现在计算机操作系统中几乎是一项必备的功能.这不单是因为计算机计算能力强大,更 ...

  3. JS OOP 概述

    JS面向对象,大致内容 1.面向对象的基础 2.深入认识JS的函数 3.JS类的实现 4JS中共有成员,私有成员和静态成员 5.JS的反射 6.JS的继承 7.JS实现抽象类 8.JS事件设计模式 9 ...

  4. java jdk1.8 API

    里面有 中英文 jdk 1.8 API    还有 jdk1.6 和1.7 英文 API 链接:https://pan.baidu.com/s/1tchABVX7htJCaO3quENP1g提取码:y ...

  5. vue 2.0 + 如何实现加入购物车,小球飞入的动画

    github源码地址:https://github.com/13476075014/node-vue/tree/master/mynodeproject/13.sell/sell 在移动端经常会有加入 ...

  6. 表格中的DOM

    通过DOM来操作table跟在html中操作table是不一样的,下面来看看怎样通过DOM来操作table. 按照table的分布来创建: <table> <thead> &l ...

  7. MyCAT详解【转】

    原文链接:MyCAT详解 作者:Rangle 一.MyCAT概述MyCAT是一款由阿里Cobar演变而来的用于支持数据库读写分离.分片的分布式中间件.MyCAT可不但支持Oracle.MSSQL.MY ...

  8. zlog日志函数库

    在C的世界里面没有特别好的日志函数库(就像JAVA里面的的log4j,或者C++的log4cxx).C程序员都喜欢用自己的轮子.printf就是个挺好的轮子,但没办法通过配置改变日志的格式或者输出文件 ...

  9. 高射炮打蚊子,杀鸡用绝世好剑:在SAP Kyma上运行UI5应用

    国人在表述"大材小用"这个场景时,总喜欢用一些实物来类比,比如:高射炮打蚊子. 英国QF 3.7英寸(94mm)高射炮,战斗全重超过9.3吨,全长近5米,最大射程约18公里,最大射 ...

  10. 4.解析配置文件 redis.conf

    将原始的redis.conf拷贝,得到一个myRedis.conf文件,修改配置文件时,就修改这个文件,不对原始的配置文件进行修改 redis配置文件中主要有以下内容: 1.units单位 a)配置大 ...