描述

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. 希尔排序——C语言

    希尔排序 希尔排序是插入排序的一种,又称“缩小增量排序”,希尔排序是直接插入排序算法的一种更高效的改进版本,关于插入排序可以看下这篇随笔:插入排序——C语言 (图片来源:https://www.cnb ...

  2. SQL语句中加中括号[ ]的作用

    有些可能是SQL里面的保留字,但是你又用了它做字段名 比如Action,用[Action] 就可以避免这个问题,如果直接Action SQL就要报错了. 解决较长的中文名表名可能会被不识别的问题.

  3. ORA-07445: exception encountered: core dump [opiaba()+639] [SIGSEGV] [ADDR:0x0] [PC:0x1858C3F] [SI_KERNEL(general_protection)] []

    开发反馈应用无法连接数据库,发现数据库实例崩溃,且数据库为11.2.0.4 单实例. 数据库告警日志发现,数据库崩溃之前出现ORA- ORA-: exception encountered: core ...

  4. java封装数据类型——Integer

    今天来学习整型 int 的封装数据类型,Integer. 1. 定义 首先来看看定义.可以看到,Integer 继承 Number 抽象类,实现了 Comparable 接口.Number 类是常用数 ...

  5. jq 停止/结束多个ajax请求

    页面按钮: <button id="song">abort</button> 请求: var str = {} str.xhr = $.ajax({ typ ...

  6. nginx的so_keepalive和timeout相关小计

    KeepAlive 这里的keepalive是TCP的探活机制: [root@ ~]# sysctl -a |grep tcp_keepalive net.ipv4.tcp_keepalive_tim ...

  7. spider _其他库的简单操作与方法

    PHP : 网络IO java : 代码笨重,代码量很大 C/C++ :虽然效率高,但是代码成型很慢 1 通用网络爬虫(搜索引擎引用,需要遵守robots协议) 1 搜索引擎如何获取一个新网站的 UR ...

  8. MySQL全同步复制基于GR集群架构实现(Centos7)

    目录 一. 理论概述 概述 二. 部署 向组加入新节点 测试 三.总结 一. 理论概述 概述 本案例操作的是针对于MySQL的复制类型中的全同步复制,对几种复制类型简单总结下: 异步复制:MySQL默 ...

  9. 微信企业红包api接入

    项目描述:基于微信浏览器的H5页面,接入微信支付接口和微信红包接口 一.接入前准备条件 1.微信公众号 需要基于已认证的微信公众号承载该H5页面.该条件默认已具备,本文重点为红包接口. 2.微信支付商 ...

  10. DNS服务和BIND

    今天我们来介绍一下DNS服务,在大家的印象中DNS可能只是将域名解析为IP地址,可能其他的暂时还不太了解,希望本篇内容能帮助大家. 1.什么是DNS? DNS( Domain Name System) ...