LeetCode:Remove Duplicates from Sorted List I II
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.
分析:和从数组中移除重复元素那一题一样的思想
/**
* 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) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if(head == NULL || head->next == NULL)return head;
ListNode *index = head, *p = head->next, *pre = head;
while(p != NULL)
{
if(p->val != pre->val)
{
index = index->next;
index->val = p->val;
}
pre = p;
p = p->next;
}
p = index->next;
index->next = NULL;
while(p != NULL)
{//销毁多余的节点
ListNode *tmp = p;
p = p->next;
delete tmp;
}
return head;
}
};
LeetCode:Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 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) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if(head == NULL)return head;
//为操作方便添加头结点
ListNode *addHead = new ListNode();
addHead->next = head;
//index 指向已经保存的最后一个节点
ListNode *index = addHead, *p = head;
while(p != NULL)
{
if(p->next == NULL || p->val != p->next->val)
{//当前节点没有重复
index = index->next;
index->val = p->val;
p = p->next;
}
else
{//当前节点有重复,找到当前节点的最后一个副本的下一个元素
while(p->next && p->val == p->next->val)
p = p->next;
p = p->next;
}
}
p = index->next;
index->next = NULL;
while(p != NULL)
{//销毁多余的节点
ListNode *tmp = p;
p = p->next;
delete tmp;
}
head = addHead->next;
delete addHead;
return head;
}
};
【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3461481.html
LeetCode:Remove Duplicates from Sorted List I II的更多相关文章
- LeetCode:Remove Duplicates from Sorted Array I II
LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...
- [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 List II 解题报告
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
- [Leetcode] Remove Duplicates From Sorted Array II (C++)
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- [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 II [27]
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- [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 Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
随机推荐
- 通过设置虚拟机(ubantu15.10)的分辨率达到全屏效果
最近搭建了一个ubantu 15.10虚拟机,怎么折腾都不能全屏显示.虽然我已经点了, 还是无法达到真正的全屏. 查了一下,http://jingyan.baidu.com/article/0964 ...
- SAM4E单片机之旅——21、DMAC之USART回显
DMAC也可以和外设进行数据交互.之前我们曾使用PDC进行USART的数据回显,这次就使用DMAC完成相同的工作.而且由于DMAC有内部的缓冲区,实现起来更为简单. 一. USART设置 因为之前已经 ...
- cocos2d-x之猜数字游戏
bool HelloWorld::init() { if ( !Layer::init() ) { return false; } visibleSize = Director::getInstanc ...
- Hibernate案例-------基于xml配置,使用Hibernate实现对员工表的增、删、改、查功能
1.1 问题 使用Hibernate实现对员工表的增.删.改.查. 1.2 方案 Hibernate使用步骤: 导入Hibernate包,以及数据库驱动包. 引入Hibernate主配置文件hiber ...
- linux command intro2 vi
vi cusor : 0 : to the beginning of the current line $ : to the end of the current line G : to the la ...
- Hive Word count
--https://github.com/slimandslam/pig-hive-wordcount/blob/master/wordcount.hql DROP TABLE myinput; DR ...
- js验证表单
在日常生活中,对于上班时间比较灵活的单位来说,如何能够及时.准确地令公司员工了解自己的上班时间是一个棘手的问题.公司管理人员不会一个挨一个地打电话通知,难免总有员工会弄错自己的上班时间.这样,可以求助 ...
- codeforces 477A A. Dreamoon and Sums(数学)
题目链接: A. Dreamoon and Sums time limit per test 1.5 seconds memory limit per test 256 megabytes input ...
- CSS设置滚动条样式
因为在现在的大部分项目中很多都用到了滚动条,有时候用到模拟的滚动条,现在说下滚动条的CSS也能解决. 比如网易邮箱的滚动条样子很好看,就是利用的CSS来设置的,而且是webkit浏览器的.如图所示: ...
- UVA 12377 Number Coding --DFS
题意:给一串数字,第一个数是Num的话,要使后面的数字组成Num个数,而且为不降的,将这Num个数分配到9个素因子上作为指数,问能组成多少个不同的数 解法:dfs一遍,看后面的数字能组成Num个不降数 ...