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. jquery对象与js对象

    对于已经是一个 DOM 对象,只需要用 $() 把DOM对象包装起来,就可以获得一个 jQuery 对象了,使用[index]和.get(index)可以转为DOM对象 jQuery 对象是通过 jQ ...

  2. javascript作用域链理解

    执行上下文(Execution context,简称EC)   概念   每当控制器到达ECMAScript可执行代码的时候,就进入了一个执行上下文.   javascript中,EC分为三种:   ...

  3. [暑假集训--数论]poj2115 C Looooops

    A Compiler Mystery: We are given a C-language style for loop of type for (variable = A; variable != ...

  4. Python之数据结构:字符串中的方法

    一.过滤字符串 1.strip() (1)去掉行收尾不可见字符 a = ' wejifrow ' print a print a.strip() 结果: wejifrow wejifrow (2)st ...

  5. h5 video切换到横屏全屏

    将video设置为屏幕大小,覆盖其他元素,想到这种操作我也是震惊的 function() { let startIcon = document.getElementById('start-icon') ...

  6. nodeJS学习(4)--- webstorm/...开发 NodeJS 项目-节1

    前提: 已安装好 IDE ,eg:webstorm/IDEA 2016.3 & 2017.1 nodeJS(含 npm 及 相应的模板等) 要用 webstorm 开发 NodeJS项目(we ...

  7. 简单实现UIlabel可复制功能

    作者 Sunshine_tt 关注 在我们日常的开发中经常会遇到一些小需求,比如需要长按控件来拷贝控件中得内容.我们知道在iOS中有三个控件自身是支持拷贝,粘贴的,如:UITextField,UITe ...

  8. linux之AWK实战【转】

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAn8AAADvCAIAAAAM1SXGAAAgAElEQVR4nO2dz8s125XXHx9oTXMHUZ

  9. DataSet导出Excel,比以往的方法导出的Excel外观更加好看

    原文发布时间为:2010-06-21 -- 来源于本人的百度文章 [由搬家工具导入] ======目前方法=========== #region 生成Excel/// <summary>/ ...

  10. 收藏一下这个微软MVP的老外博客

    原文发布时间为:2011-04-20 -- 来源于本人的百度文章 [由搬家工具导入] http://blog.overridethis.com/blog  http://haacked.com htt ...