leetcode83 Remove Duplicates from Sorted List
题意:删掉单链表里重复的节点,如:
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.
思路:真的真的是很简单的题啊,但是有个陷阱,也怪自己练的太少,都忘记了这个。思路其实就是判断如果p->val == p->next->val就删掉p->next节点,如果新的p->next的值与p的值不相等则往后移动。这里的判断条件有两个,
一个是p->val != p->next->val,一个是p->next != NULL。
代码:
ListNode* deleteDuplicates(ListNode* head)
{
ListNode* p = head;
if(head == NULL || head->next == NULL)
return head;
while(p->next != NULL)
{
if(p->val == p->next->val)
{
ListNode* temp = p->next;
p->next = p->next->next;
free(temp);
}
if(p->next != NULL && p->val != p->next->val)
{
p = p->next;
}
}
return head;
}
leetcode83 Remove Duplicates from Sorted List的更多相关文章
- Leetcode-83 Remove Duplicates from Sorted List
#83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such that ...
- [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 II 移除有序链表中的重复项之二
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
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 and ret ...
- Remove Duplicates from Sorted List II
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
- Remove Duplicates From Sorted Array
Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 26. Remove Duplicates from Sorted Array
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
随机推荐
- NOIP2012 洛谷P1084 疫情控制
Description: H 国有 n 个城市,这 n 个城市用 n-1 条双向道路相互连通构成一棵树,1 号城市是首都,也是树中的根节点. H 国的首都爆发了一种危害性极高的传染病.当局为了控制疫情 ...
- 如何写出规范的JavaScript代码
作为一名开发人员(WEB前端JavaScript开发),不规范的开发不仅使日后代码维护变的困难,同时也不利于团队的合作,通常还会带来代码安全以及执行效率上的问题.本人在开发工作中就曾与不按规范来开发的 ...
- Nginx的火速蔓延与其并发性处理优势
Nginx是俄罗斯人编写的十分轻量级的HTTP服务器.Nginx,它的发音为“engine X”, 是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP 代理服务器.Ngi ...
- 第116讲 boost::algorithm::string之替换和删除
http://www.360doc.com/content/16/0523/18/29304643_561672752.shtml
- wget命令下载FTP整个目录进行文件备份
使用wget下载整个FTP目录,可以用于服务器间文件传输,进行远程备份.通过限制网速,可以解决带宽限制问题. #wget ftp://IP:PORT/* --ftp-user=xxx --ftp-pa ...
- java基础知识(二)-----多态和构造函数
一:前言 最近由于面试了新浪公司,面试官问我的问题我都不知道,觉得自己好菜,所以最近决定再把java基础给搞一遍,真的觉得自己好菜.每天看一点,那个家伙说<java编程思想>最少要看三遍, ...
- 修改nginx对http请求数据大小限制
1. 问题发现 在公司搭建了一个基于mindoc的wiki知识库,用nginx做的反向代理服务器,同事在使用过程中上传某个文件一直失败,于是看着看下mindoc自己的日志文件,发现都是类似于fastd ...
- 【BZOJ4774】修路 [斯坦纳树]
修路 Time Limit: 20 Sec Memory Limit: 256 MB Description Input Output 仅一行一个整数表示答案. Sample Input 5 5 2 ...
- Codeforces Round #299 Div2 解题报告
这场比赛并没有打现场,昨天晚上做了ABCD四道题,今天做掉了E题 以前还没有过切完一场比赛的所有题呢~爽~ A. Tavas and Nafas Today Tavas got his test ...
- [bzoj4766]文艺计算姬——完全二分图生成树个数
Brief Description 求\(K_{n,m}\) Algorithm Design 首先我们有(Matrix Tree)定理,可以暴力生成几组答案,发现一些规律: \[K_{n,m} = ...