【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 ...
随机推荐
- ELK日志分析系统(转)
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://467754239.blog.51cto.com/4878013/1700828 ...
- 微信事业群WXG成立 致力于打造微信大平台
今天,微信之父张小龙带领微信团队成立微信事业群(WeiXin Group,简称WXG),致力于打造微信大平台,由他们负责微信基础平台.微信开放平台.微信支付拓展.O2O等微信延伸业务的发展,并包括邮箱 ...
- Wdcp在安装memcached出现错误的解决办法
今天在安装memcached时出现了以下错误(tar: libevent-1.4.14b-stable.tar.gz: Cannot open: No such file or directory), ...
- linux查找某一进程并杀死
1. 查找redis进程 ps -ef|grep redis-server 2.打印第二个参数,因为上面第二列是进程号 3.这两个进程号有一个是grep进程号,所以要去掉,反选 grep ps ...
- 8-IO总结
3. 4. 5.
- Android三种消息提示
Android消息提示有三种方式: 1 使用Toast显示消息提示框 Toast类用于在屏幕中显示一个提示信息框,该消息提示框没有任何控制按钮,并且不会获得焦点,经过一定时间后自动消失.通常用于显示 ...
- Redis学习笔记二:单机数据库的实现
1. 数据库 服务器中的数据库 Redis服务器将所有数据库都保存在服务器状态redis.h/redisServer结构的db数组中,db数组的每个项都是一个redis.h/redisDb结构,每个r ...
- oracle 彻底删除用户及表空间
1.删除表空间 可以先将其offline alter tablespace xxx offline; 将磁盘上的数据文件一同删除 drop tablespace xxx including conte ...
- [OpenJudge0054]特务会议召开
[OpenJudge0054]特务会议召开 试题描述 在敌占区的特务时常会碰头.敌占区有n个城市,为保证安全,中央经过侦查,只选择了n-1条较安全的道路作为特务们碰头可以走的道路.每次开会,中央会选择 ...
- 如何安装PANABIT?
PANABIT可以帮你轻松的封杀360WIFI.二级路由和移动设备,还可以让员工在上班期间禁止浏览与工作无关的网站,禁止看视频,禁止聊QQ,禁止网购......总之一切你能想到的,它都能帮你实现. P ...