leetcode解题报告(6):Remove Duplicates from Sorted List
描述
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) {
ListNode* tmp = head;
while(tmp){
while(tmp -> next && tmp -> val == tmp -> next -> val)
tmp -> next = tmp -> next -> next;
tmp = tmp -> next;
}
return head;
}
};
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(head == NULL) return NULL;
ListNode* pre = head,*cur = head -> next;
while(cur){
if(pre -> val == cur -> val){
pre -> next = cur -> next;
cur = cur -> next;
}else{
pre -> val = cur -> val;
pre = pre -> next;
cur = cur -> next;
}
}
};
leetcode解题报告(6):Remove Duplicates from Sorted List的更多相关文章
- LeetCode之“链表”:Remove Duplicates from Sorted List && Remove Duplicates from Sorted List II
1. Remove Duplicates from Sorted List 题目链接 题目要求: Given a sorted linked list, delete all duplicates s ...
- <LeetCode OJ> 83. Remove Duplicates from Sorted List
83. Remove Duplicates from Sorted List Total Accepted: 94387 Total Submissions: 264227 Difficulty: E ...
- leetCode练题——26. Remove Duplicates from Sorted Array
1.题目 26. Remove Duplicates from Sorted Array--Easy Given a sorted array nums, remove the duplicates ...
- LeetCode(80)Remove Duplicates from Sorted Array II
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- 【LeetCode算法-26】Remove Duplicates from Sorted Array
LeetCode第26题 Given a sorted array nums, remove the duplicates in-place such that each element appear ...
- leetcode第26题--Remove Duplicates from Sorted Array
problem: Given a sorted array, remove the duplicates in place such that each element appear only onc ...
- LeetCode(28)-Remove Duplicates from Sorted Array
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- LeetCode记录之26——Remove Duplicates from Sorted Array
国外的表达思维跟咱们有很大差别,做这道题的时候很明显.简单说本题就是让你把有序数组中的重复项给换成正常有序的.比如 1 2 2 3换成 1 2 3 3,根本不需要考虑重复的怎么办,怎么删除重复项等等. ...
- LeetCode(82)Remove Duplicates from Sorted List
题目 Given a sorted linked list, delete all duplicates such that each element appear only once. For ex ...
- LeetCode(26) Remove Duplicates from Sorted Array
题目 Given a sorted array, remove the duplicates in place such that each element appear only once and ...
随机推荐
- opencv-01--图像的遍历
遍历图像的4种方式 一.at<typename>(i,j) Mat类提供了一个at的方法用于取得图像上的点,它是一个模板函数,可以取到任何类型的图像上的点.下面我们通过一个图像处理中的实际 ...
- c#基础知识梳理(二)
上期回顾 - https://www.cnblogs.com/liu-jinxin/p/10818256.html 一.变量 一个变量只不过是一个供程序操作的存储区的名字.在 C# 中,每个变量都有一 ...
- CUDA 笔记
名词解释 SM :Streaming Multiprocessor 而 Block 大致就是对应到 SM 所有的blocks 按照流水线被送到6个SM中进行计算 在 Compute Ca ...
- restTemplate源码解析(四)执行ClientHttpRequest请求对象
所有文章 https://www.cnblogs.com/lay2017/p/11740855.html 正文 上一篇文章中,我们创建了一个ClientHttpRequest的实例.本文将继续阅读Cl ...
- vue,onerror实现当图片加载失败时使用默认图
1. 2.
- perl判断文件是否存在 perl -e
perl 有很多的命令 其中有 -e 是判断文件和目录是否存在 代码如下: #!/usr/bin/perl $fileExist = -e "/var/log/messages&qu ...
- SQLiteDatabase执行update、insert操作的时候,conflictAlgorithm参数的含义区别
/** * When a constraint violation occurs, an immediate ROLLBACK occurs, * thus ending the current tr ...
- SpringCloud之Zuul网关原理及其配置
Zuul是spring cloud中的微服务网关.网关: 是一个网络整体系统中的前置门户入口.请求首先通过网关,进行路径的路由,定位到具体的服务节点上. Zuul是一个微服务网关,首先是一个微服务.也 ...
- python3 random
一.random 1.生成伪随机数 2.伪随机数是可预测的,严格意义上不具有随机性质,通常用数学公式的方法(比如统计分布,平方取中等)获得 3.正如数列需要有首项,产生伪随机数需要一个初值用来计算整个 ...
- 三种方法给Vmware虚拟机占用空间清理瘦身
随着VMware虚拟机使用时间的增长,其所占用的空间也越来越大,本文来说说怎么给VMware虚拟机占用的空间进行瘦身. 方法一:VMware自带的清理磁盘这个方法是VMware自带,具有普适性,对快照 ...