83. Remove Duplicates from Sorted List

Total Accepted: 94387 Total
Submissions: 264227 Difficulty: Easy

题目意思:如今有一个已经排好顺序的链表,删除全部反复的节点。使每一个节点都仅仅出现一次!

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==NULL)
return NULL;
ListNode* preNode=head;
ListNode* curNode=head->next;
ListNode* delNode=NULL;
while(curNode)
{
if(preNode->val==curNode->val)
{
preNode->next=curNode->next;
delNode=curNode;
curNode=curNode->next;
delete delNode;
delNode=NULL;
continue;
}
preNode=curNode;
curNode=curNode->next;
} return head;
}
};

注:本博文为EbowTang原创。兴许可能继续更新本文。

假设转载,请务必复制本条信息。

原文地址:http://blog.csdn.net/ebowtang/article/details/50483226

原作者博客:http://blog.csdn.net/ebowtang

<LeetCode OJ> 83. Remove Duplicates from Sorted List的更多相关文章

  1. LeetCode OJ 83. Remove Duplicates from Sorted List

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

  2. 【一天一道LeetCode】#83. Remove Duplicates from Sorted List

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  3. LeetCode OJ 82. Remove Duplicates from Sorted List II

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  4. LeetCode OJ 80. Remove Duplicates from Sorted Array II

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

  5. LeetCode OJ:Remove Duplicates from Sorted Array II(移除数组中的重复元素II)

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

  6. 【LeetCode】83 - Remove Duplicates from Sorted Array

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

  7. LeetCode OJ 26. Remove Duplicates from Sorted Array

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

  8. 【LeetCode OJ】Remove Duplicates from Sorted Array

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

  9. LeetCode OJ:Remove Duplicates from Sorted Array(排好序的vector去重)

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

  10. 【LeetCode】83. Remove Duplicates from Sorted List 解题报告(C++&Java&Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 判断相邻节点是否相等 使用set 使用列表 递归 日 ...

随机推荐

  1. Python面向对象之常用的特殊方法(5)

    Python面向对象里面有很多特殊方法,例如__init__(构造方法),__del__(析构方法),这些方法对于面向对象编程非常重要,下面列出一些常用的特殊方法 (1)__call__ class ...

  2. LINUX SHELL脚本攻略笔记[速查]

    Linux Shell脚本攻略笔记[速查] 资源 shell script run shell script echo printf 环境变量和变量 pgrep shell数学运算 命令状态 文件描述 ...

  3. 用Vundle管理Vim插件

    作为程序员,一个好用的Vim,是极其重要的,而插件能够使原本功能羸弱的Vim变得像其他功能强大的IDE一样好用.然而下载.配置插件的过程比较繁琐,大家往往需要自己进行下载/配置等操作,如果还涉及到更新 ...

  4. BZOJ 3876 支线剧情(有上下界的无源汇最小费用可行流)

    3876: [Ahoi2014]支线剧情 Time Limit: 10 Sec  Memory Limit: 256 MB Submit: 1783  Solved: 1079 [Submit][St ...

  5. shell的使用

    删除 http://blog.csdn.net/u011579204/article/details/46709929 #! /bin/bash dir=/webserver/test/(等号之间不能 ...

  6. ashx输出文字,执行JavaScript,输出图片等

    原文发布时间为:2009-09-30 -- 来源于本人的百度文章 [由搬家工具导入] 一提到Ashx文件,我们就会想到http handler以及图片加载(在之前我们一般使用ASPX或者Webserv ...

  7. 【Linux】自主实现my_sleep【转】

    转自:http://blog.csdn.net/scenlyf/article/details/52068522 版权声明:本文为博主原创文章,未经博主允许不得转载. 首先说一下信号相关的内容. ** ...

  8. linux内核情景分析之命名管道

    管道是一种"无名","无形文件,只可以近亲进程使用,不可以再任意两个进程通信使用,所以只能实现"有名","有形"的文件来实现就可以 ...

  9. usb 2.0 支援的速度

    from http://www.usb.org/developers/docs/usb20_docs/ high speed : 480 Mb/s full speed : 12 Mb/s low s ...

  10. paramiko模块及ssh远程登陆

    ssh实现远程登陆一般有两种方式,一种就是用户密码登陆,另一种是密钥登陆(当然默认是要服务端打开ssh服务). 我这里使用这两种方法操作一下远程登陆,测试客户端是本机的root与jeff用户,远程连接 ...