【leetcode】 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.
给出一个已排序链表,删除所有重复的元素使每一个节点值只出现一次
思路:
1. 定义pCurNode,pNextNode两个节点指针;
2. pCurNode初始化为链表头节点指针;pNextNode初始化为下一节点
3. 判断pCurNode是否有效;
4. 判断相邻两个节点是否相同,如相同删除后一个相同的节点(其实就是将pCurNode的next指针指向删除节点的下一节点即可),再循环比较原来删除节点后的节点值是否相同。如(1, 1)、(1,1, 1);
5. 如相邻两个节点不同,则将当前节点指针后移一个节点;
6. 重复3、4、5;
class Solution
{
public:
ListNode* deleteDuplicates(ListNode* head)
{
ListNode* pCurNode = head;
ListNode* pNextNode = nullptr;
while (pCurNode)
{
pNextNode = pCurNode->next;
if (pNextNode&&pNextNode->val == pCurNode->val)
{
pCurNode->next = pNextNode->next;
}
else
{
pCurNode = pNextNode;
}
} return head;
} void Print(ListNode* head)
{
ListNode* pCurNode = head;
while (pCurNode)
{
printf(" %d", pCurNode->val);
pCurNode = pCurNode->next;
} printf("\n");
}
};
【leetcode】 Remove Duplicates from Sorted List的更多相关文章
- 【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
题目描述: 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 (easy)
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 ...
- 【LeetCode】Remove Duplicates from Sorted List(删除排序链表中的重复元素)
这道题是LeetCode里的第83道题. 题目描述: 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 1->2 示例 2: ...
随机推荐
- (zhuan) Deep Reinforcement Learning Papers
Deep Reinforcement Learning Papers A list of recent papers regarding deep reinforcement learning. Th ...
- C# 指定Webbrowser控件所用IE内核版本
如果电脑上安装了IE8或者之后版本的IE浏览器,Webbrowser控件会使用IE7兼容模式来显示网页内容.解决方法是在注册表中为你的进程指定引用IE的版本号. 比如我的程序叫做a.exe,以64位机 ...
- getview不执行
<FrameLayout android:layout_width="match_parent" android:layout_height="match_pare ...
- go 语言中常用的包
来自学习go语言.pdf 译者刑星 ==== fmt 包fmt实现了格式化IO函数,这与c的printf和scanf类似,格式化短语派生于c %v 默认格式的值.当打印结构时,加号(%+v)会增加字段 ...
- SQLServer修改表所有者
当用sp_adduser 对数据库进行添加用户之后.却出现了此对象 '表名' 无效的现象? 执行这个语句,就可以把当前库的所有表的所有者改为dboexec sp_msforeachtable 'sp_ ...
- MSSTDFMT.DLL无法注册的解决
今天在使用Windows8的时候,发现了一个问题,当我想执行某个xxx.exe文件的时候,报的问题是MSSTDFMT.DLL无法注册. 但是我的系统又是64位的,那么可以这样操作: 从网上下载一个ms ...
- coderforces 731c
题目大意:给出m组数据,每组数据包括两个数Li与Ri,分别表示左右袜子的索引(下标),表示这一天要穿的袜子:而我们要使得每天穿的这两只袜子的颜色相同,所以可以改变袜子的颜色,每次只能改变一只袜子的颜色 ...
- OAF_文件系列6_实现OAF导出XML文件javax.xml.parsers/transformer(案例)
20150803 Created By BaoXinjian
- codeforces E. Famil Door and Roads 期望
一棵树,n个节点,边长为1,有q个询问,每个询问给出u,v(u != v),问在树上等概率加一条边,如果使得u,v在一个环内,则这种加边方式是合法的,此时的值为环的长度,所有合法的加边方式出现的概率相 ...
- inteview que2
1.spring的缓存,mybatis缓存a.基于注解的方式 三种注解b.mybatis分为一级session和二级缓存mapperc.采用LRU算法(近期最少使用) http://www.iteye ...