题目:

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. hdu 2063过山车

    二分匹配 #include<iostream> #include<cstring> using namespace std; int k,m,n; int rem[500+5] ...

  2. Redis专题(2):Redis数据结构底层探秘

    前言 上篇文章Redis闲谈(1):构建知识图谱介绍了redis的基本概念.优缺点以及它的内存淘汰机制,相信大家对redis有了初步的认识.互联网的很多应用场景都有着Redis的身影,它能做的事情远远 ...

  3. request 防盗链

    package request; import java.io.IOException;import javax.servlet.ServletException;import javax.servl ...

  4. [概念理解] MVC模式和C++的实现

    [转]学习可以是一件很快乐的事,特别是当你发现以前所学的点点滴滴慢慢地能够串起来或者变成了一个环,这种感觉真好.这篇文章就这么来的. 从MVC架构开始说起吧.这两天系统了解了一下MVC架构的内容,主要 ...

  5. WCF服务返回XML或JSON格式数据

    第一种方式public string GetData( string format) { string res = null; Student stu = new Student { StuID = ...

  6. shader学习之一:Properties语义块支持的数据类型

    _Int ("Int",Int)=2为:变量名("面板显示的名称",数据类型) 对于Int,Float,Range这些数字类型的属性,默认值为单独的数字.对于贴 ...

  7. 说明sizeof和strlen之间的区别。

    解析:由以下几个例子我们说明sizeof和strlen之间的区别.第1个例子: sizeof(ss)结果为4,ss是指向字符串常量的字符指针.sizeof(*ss)结果为1,*ss是第一个字符.第2个 ...

  8. EasyPlayerPro Windows播放器电子放大/局部放大播放功能实现

    背景描述 在视频监控软件中,我们看到很多的软件都有电子放大功能, 按住鼠标左键不放,框选一个区域,再松开鼠标左键,即对选中的区域进行放大显示, 且可以重复该操作,逐步放大所需显示的区域, 有没有觉得, ...

  9. Mybatis之基本简介

    一.Mybatis简介 MyBatis 是一个可以自定义SQL.存储过程和高级映射的持久层框架.MyBatis 摒除了大部分的JDBC代码.手工设置参数和结果集重获.MyBatis 只使用简单的XML ...

  10. Dubbo服务集群,常见容错机制:failover ,failsafe,failfase ,failback,forking

    http://blog.csdn.net/hongweigg/article/details/52925920 http://m.blog.csdn.net/article/details?id=51 ...