【leetcode】Remove Duplicates from Sorted List (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.
思路:
简单题,没什么好说的。
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
if(head == NULL)
return NULL;
ListNode * ans = head;
ListNode * anscur = ans; //记录答案链表的最后一个指针
ListNode * cur = head->next; //记录判断的是原链表中的哪一个指针
while(cur != NULL)
{
if(anscur->val != cur->val) //只有值变化的时候才把指针接到ans链表后面 节省操作
{
anscur->next = cur;
anscur = anscur->next;
}
cur = cur->next;
}
anscur->next = NULL; //防止最后的指针是重复的,给ans结尾
return ans;
}
};
大神的代码更加简洁, 没有必要新建一个头结点,就用原来的头结点就好。
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
if(NULL == head) return head;
ListNode *cur = head, *nxt = head->next;
while (nxt) {
if (cur->val != nxt->val)
cur = cur->next = nxt;
else if (!nxt->next)
cur->next = nxt->next;
nxt = nxt->next;
}
return head;
}
};
【leetcode】Remove Duplicates from Sorted List (easy)的更多相关文章
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 【leetcode】Remove Duplicates from Sorted Array I & II(middle)
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- leetcode 之Remove Duplicates from Sorted Array(2)
描述 Follow up for ”Remove Duplicates”: What if duplicates are allowed at most twice? For exampl ...
- 【leetcode】Remove Duplicates from Sorted Array
题目描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ...
- 【leetcode】Remove Duplicates from Sorted List
题目简述 Given a sorted linked list, delete all duplicates such that each element appear only once. For ...
- 【leetcode】 Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- 【Leetcode】Remove Duplicates from Sorted List in JAVA
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- 【leetcode】Remove Duplicates from Sorted List II (middle)
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- 【Leetcode】Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
随机推荐
- 【AngularJS】—— 1 初识AngularJs
怀着激动与忐忑的心情,开始了学习AngularJS的旅程,很久之前就听说了这个前端框架,但是由于自己一直没有从事相关的工作,因此也没有进行学习.这次正好学习AngularJS,直接复习一下前端的知识. ...
- AngularJS API之toJson 对象转为JSON
toJson()能把对象序列化为json 方法讲解 这个方法最多支持2个参数: angular.toJson(obj, pretty); obj 是想要转换的对象, pretty 可以调节格式化的样式 ...
- QS2016年全球高等教育系统实力排名 中国排名世界第八亚洲第一
2016年5月18日,QS发布"2016年全球高等教育系统实力排名",中国在此榜单表现优异,排名世界第八亚洲第一. 排名指标 排名指标及计算方法如下: 系统实力:QS大学排名前70 ...
- 数据库左连接left join、右连接right join、内连接inner join on 及 where条件查询的区别
join on 与 where 条件的执行先后顺序: join on 条件先执行,where条件后执行:join on的条件在连接表时过滤,而where则是在生成中间表后对临时表过滤 left joi ...
- 设置p标签自动换行
<body> <p style="width:20px;height:100px;background-color:#069; word-wrap: break-w ...
- java之BASE64加解密
1.简介 Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,采用Base64编码具有不可读性,即所编码的数据不会被人用肉眼所直接看到. 注:位于jdk的java.util包中. 2. ...
- php面向对象面试题
php面试题之四--PHP面向对象(基础部分) 四.PHP面向对象 1. 写出 php 的 public.protected.private 三种访问控制模式的区别(新浪网技术部) public:公有 ...
- Cocos2d-x 3.0 Json用法 Cocos2d-x xml解析
Cocos2d-x 3.0 加入了rapidjson库用于json解析.位于external/json下. rapidjson 项目地址:http://code.google.com/p/rapidj ...
- qt-5.6.0 移植之纯净的linux文件系统的建立
为什么要建立一个最纯净的文件系统,一开始是想在qt-4.8.5的文件系统基础之上加东西,慎重想了一下,这方法行不通,以为有很多东西不熟悉.干脆就自己建立一个. 步骤很简单: 一:下载一个bulidro ...
- 获取Spring容器Bean
WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext(); ManageResolver mr = (Ma ...