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 ...
随机推荐
- Linux写时拷贝技术(copy-on-write)
COW技术初窥: 在Linux程序中,fork()会产生一个和父进程完全相同的子进程,但子进程在此后多会exec系统调用,出于效率考虑,linux中引入了“写时复制“技术,也就是只有进程空间的各段的内 ...
- 关于Tomcat启动时报The APR based Apache Tomcat Native library which allows optimal performanc e in production environments was not found on the java.library.path
错误信息如下 八月 01, 2016 10:11:15 上午 org.apache.catalina.core.AprLifecycleListener initINFO: The APR based ...
- 采用 PAT工具及CSP语言,对一个问题进行自动机 建模
pat是新加坡国立开发的工具,需要的去官网下http://www.comp.nus.edu.sg/~pat/ ,学了一天,是个不错的自动机验证工具,感觉还不错啊. 验证一个数是否为斐波那契数且为质数 ...
- Asp.Net远程调试
1.在本地找到VS安装目录下的 Visual Studio Tools 文件夹 并进入Remote Debugger Folder文件夹 2.根据服务器的操作系统是32位还是64位,选择下面的文件夹 ...
- Struts2 Spring Hibernate等各个版本下载推荐
推荐jar包下载地址: http://mvnrepository.com/ 应有尽有
- 图解SQL的inner join、left join、right join、full outer join、union、union all的区别
转自:http://blog.csdn.net/jz20110918/article/details/41806611 假设我们有两张表.Table A 是左边的表.Table B 是右边的表.其各有 ...
- hibernate一对一关系实现
按照主键映射,按照外键映射 Address.hbm.xml: <?xml version="1.0"?><!DOCTYPE hibernate-mapping P ...
- uname
uname uname用于打印操作系统和硬件架构相关的信息,对于可能在多个系统或架构上运行的Shell脚本程序很有用, 缺省选项相当于 -s 或--system $uname [-amnrsvpio] ...
- Linux Crash/Hang on Bay Trail/J1900/N2940
近几年的linux kernel, 尤其是4.1以后,在Bay Trail平台上会随机挂起和死机,亲测j1900,死机非常频繁,而且死机前毫无征兆,直接就挂起了,console也没有相应. 这个问题在 ...
- html,js简单保存textarea换行格式
有时候我们在做表单提交时,往往需要把html标签保存起来,但是textarea不保存换行的信息,所以我们需要用js来实现保存textarea的换行等HTM标签.真正让HTML文本框里的换换等格式保留下 ...