题目:

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

Example

Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.

题解:

Solution 1 ()

class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
ListNode* cur = head;
while (cur != nullptr && cur->next != nullptr) {
if (cur->val == cur->next->val) {
cur->next = cur->next->next;
continue;
}
cur = cur->next;
}
return head;
}
};

Solution 2 ()

class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(!head || !head->next) return head; ListNode *front, *last; front = head;
last = head->next; while(last)
{
if(front->val == last->val)
{
front->next = last->next;
delete last;
last = front->next;
}
else
{
front = last;
last = last->next;
}
} return head; }
};

【Lintcode】112.Remove Duplicates from Sorted List的更多相关文章

  1. 【Lintcode】113.Remove Duplicates from Sorted List II

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

  2. 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  3. 【LeetCode】080. Remove Duplicates from Sorted Array II

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

  4. 【LeetCode】82. Remove Duplicates from Sorted List II 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/remove-du ...

  5. 【leetcode】 26. Remove Duplicates from Sorted Array

    @requires_authorization @author johnsondu @create_time 2015.7.22 18:58 @url [remove dublicates from ...

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

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

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

  8. 【LeetCode】26. Remove Duplicates from Sorted Array

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

  9. 【LeetCode】026. Remove Duplicates from Sorted Array

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

随机推荐

  1. 数据结构(Java语言)——Stack简单实现

    栈是限制插入和删除仅仅能在一个位置上进行的表.该位置是表的末端,叫做栈的顶top.对栈的基本操作有进栈push和出栈pop,前者相当于插入.后者这是删除最后插入的元素. 栈有时又叫先进先出FIFO表. ...

  2. java String概述

    class StringDemo { public static void main(String[] args)  {  String s1 = "abc";//s1 是一个类类 ...

  3. Color.js 方便修改颜色值

    这并不是npm上比较活跃的clolr包的中文文档,不过它在最后提到了: The API was inspired by color-js. Manipulation functions by CSS ...

  4. IOS获取当前地理位置文本

    本文转载至  http://blog.csdn.net/lvxiangan/article/details/28101119   以下内容摘抄自网络,著作权属于原作者 方法1:使用ios自带联网查询功 ...

  5. static 不被实例调用

    static - JavaScript | MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/ ...

  6. vscode设置默认shell 快速到行

    vscode设置默认shell - CSDN博客 https://blog.csdn.net/butterfly5211314/article/details/78944805 在文件 -> 首 ...

  7. mysq'l系列之10.mysql优化&权限控制

    网站打开慢如何排查 1.打开网页, 用谷歌浏览器F12, 查看network: 哪个加载时间长就优化哪个 2.如果是数据库问题 2.1 查看大体情况 # top # uptime  //load av ...

  8. 【题解】Codeforces 961G Partitions

    [题解]Codeforces 961G Partitions cf961G 好题啊哭了,但是如果没有不小心看了一下pdf后面一页的提示根本想不到 题意 已知\(U=\{w_i\}\),求: \[ \s ...

  9. 对A轮的追逐变得越加狂热,当前距离互联网泡沫到底有多近?

    编者注:本文来自TOMASZ TUNGUZ,中文版由天地会珠海分舵进行编译. 依据NVCA披露的最新数据,在2015年第二季度.VC总共进行了167亿美元的投资,大约是2000年互联网泡沫时候同期的6 ...

  10. 一起来学linux:SAMBA服务器搭建

    前面介绍的NFS服务器的用来linux和linux系统之间共享文件和目录的,那如果是linux和windows之间需要共享修改文件该如何操作呢.这据需要用到SAMBA系统.我们首先来看下SAMBA系统 ...