【Remove Duplicates from Sorted List 】cpp
题目:
第一次刷的时候漏掉了这道题。
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 ) return head;
ListNode dummpy(-);
dummpy.next = head;
ListNode* pre = head;
ListNode* curr = head->next;
while (curr)
{
if ( curr->val!=pre->val )
{
pre = curr;
curr = curr->next;
}
else
{
pre->next = curr->next;
curr = curr->next;
}
}
return dummpy.next;
}
};
tips:
纠结了一下才AC。原因是第一次写的时候:
pre = curr;
curr = curr->next;
【Remove Duplicates from Sorted List 】cpp的更多相关文章
- 【Remove Duplicates from Sorted Array】cpp
题目: https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove ...
- leetcode 【 Remove Duplicates from Sorted Array 】python 实现
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- leetcode 【 Remove Duplicates from Sorted List 】 python 实现
题目: Given a sorted linked list, delete all duplicates such that each element appear only once. For e ...
- 【Remove Duplicates from Sorted Array II】cpp
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- leetcode 【 Remove Duplicates from Sorted Array II 】python 实现
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- 【Remove Duplicates from Sorted List II 】cpp
题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct ...
- leetcode 【 Remove Duplicates from Sorted List II 】 python 实现
题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct ...
- 【Median of Two Sorted Arrays】cpp
题目: There are two sorted arrays A and B of size m and n respectively. Find the median of the two sor ...
- 【Search In Rotated Sorted Array】cpp
题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...
随机推荐
- java IO流——字符流
一.概述 流的概念: 流是个抽象的概念,是对输入输出设备的抽象,Java程序中,对于数据的输入/输出操作都是以“流”的方式进行.设备可以是文件,网络,内存等. 流具有方向性,至于是输入流还是输出流则是 ...
- Git入门体验
Git这个东西我也是最近才知道的,然后知道后却发现一个事实:自己真的是太LOW啦!竟然连Git都不知道!!!??? Git 在实际协同工作时会为我们提供巨大帮助, 下面简单介绍一下Git的用法: 一. ...
- S/4HANA for Customer Management里的搜索分页处理
这篇文章的英文版我发在了SAP Community上:Paging Implementation in S/4HANA for Customer Management https://blogs.sa ...
- OpenCV2马拉松第5圈——线性滤波
收入囊中 这里的非常多内容事实上在我的Computer Vision: Algorithms and ApplicationsのImage processing中都有讲过 相关和卷积工作原理 边界处理 ...
- Entity Framework 连接 mysql 。(code first模式)
准备工作 1.下载vs2015 2.下载mysql2017 3.安装 1.创建类库 . 2.打开Nuget包,下载最新版的entity framewor. 3.在引用中添加 mysql.data; m ...
- 错误:javax.servlet.http.HttpServlet" was not found on the Java Build Path
我们在用Eclipse进行Java web开发时,可能会出现这样的错误: The superclass javax.servlet.http.HttpServlet was not found on ...
- 【JS-Java-EL】JavaScript和Java(EL表达式)引发的 Uncaught SyntaxError: Unexpected token ILLEGAL
2018.10.14 BUG原因: 在较早期的代码中,容易出现 JS 拼接 HTML 代码字符串的情况.如 // 页面 test.jsp 内部的 JS 代码 // ${} JSP中EL语法,内部为Ja ...
- mysql完全卸载大全
如何在Linux下卸载MySQL数据库呢? 下面总结.整理了一下Linux平台下卸载MySQL的方法. MySQL的安装主要有三种方式:二进制包安装(Using Generic Binaries).R ...
- 网际协议 IP
网际协议 网际协议(internet protocol),简称IP; 概念:TCP/IP网络体系结构中网际层的协议.用以提供无连接的数据服务. 1.IP地址的概念及组成 概念:IP地址就是用来唯一标 ...
- JDK 动态代理 讨债实例
现弄一个讨债接口 package cn.itcast.g_dongtaidaili; public interface Taozhai { void taozhai(); } 再弄一个讨债实现类 pa ...