remove-duplicates-from-sorted-list I&II——去除链表中重复项
I、Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given1->1->2, return1->2.
Given1->1->2->3->3, return1->2->3.
PS:遍历,而后记录pre,并删除后续重复node
/**
* 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) {
ListNode *cur=head,*pre=NULL;
while(cur!=NULL){
if(pre==NULL){
pre=cur;
cur=cur->next;
continue;
}
ListNode *next=cur->next;
if(pre->val==cur->val){
pre->next=next;
}else
pre=cur;
cur=next;
}
return head;
}
};
II、
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given1->2->3->3->4->4->5, return1->2->5.
Given1->1->1->2->3, return2->3.
删除所有重复项。
PS:循环比较next->val==cur->val,若next跳动则剔除cur至next之间的节点。否则右移left指针。
/**
* 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) {
if(head==NULL) return NULL;
ListNode h(-);
ListNode *res=&h;
res->next=head;
ListNode *cur=head,*pre=NULL,*left=res;
while(cur!=NULL){
ListNode* next=cur->next;
while(next!=NULL&&cur->val==next->val){
next=next->next;
}
if(next==cur->next){
left=cur;
}else{
left->next=next;
}
cur=next;
}
return res->next;
}
};
remove-duplicates-from-sorted-list I&II——去除链表中重复项的更多相关文章
- LeetCode 83. 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 移除有序链表中的重复项
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- [LeetCode] 83. Remove Duplicates from Sorted List 移除有序链表中的重复项
Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1 ...
- 【LeetCode每天一题】Remove Duplicates from Sorted List(移除有序链表中的重复数字)
Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1 ...
- [LC]83题 Remove Duplicates from Sorted List(删除排序链表中的重复元素)(链表)
①英文题目 Given a sorted linked list, delete all duplicates such that each element appear only once. Exa ...
- 力扣—Remove Duplicates from Sorted List(删除排序链表中的重复元素)python实现
题目描述: 中文: 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2输出: 1->2 示例 2: 输入: 1->1->2 ...
- LeetCode 83. Remove Duplicates from Sorted List(从有序链表中删除重复节点)
题意:从有序链表中删除重复节点. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode ...
- [CareerCup] 2.1 Remove Duplicates from Unsorted List 移除无序链表中的重复项
2.1 Write code to remove duplicates from an unsorted linked list.FOLLOW UPHow would you solve this p ...
- LeetCode:Remove Duplicates from Sorted List I II
LeetCode:Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such t ...
随机推荐
- python相关的编码,运算
一 字符串的格式化 python中使用占位符%来实现. name=input('name:') age=input('age:') hobby=input('hobby:') print('%s的年龄 ...
- 解决ie8下页面刚出现时候的晃动问题
出现这个问题的原因的页面的高度超过一屏,这个时候需要在开始的时候给 html,body {overflow:scroll;overflow-x:hidden}; 这样就可以解决这个问题了
- 【04】Vue 之 事件处理
4.1. 监听事件的Vue处理 Vue提供了协助我们为标签绑定时间的方法,当然我们可以直接用dom原生的方式去绑定事件.Vue提供的指令进行绑定也是非常方便,而且能让ViewModel更简洁,逻辑更彻 ...
- CodeForces Round #402 (Div.2) A-E
2017.2.26 CF D2 402 这次状态还算能忍吧……一路不紧不慢切了前ABC(不紧不慢已经是在作死了),卡在D,然后跑去看E和F——卧槽怎么还有F,早知道前面做快点了…… F看了看,不会,弃 ...
- POJ1195Mobile phones
二维树状数组板子题. #include<cstdio> #include<cstring> #include<iostream> #include<cstdl ...
- Maven 集成Tomcat7插件自动部署
1.配置tomcat-users.xml文件 在tomcat安装目录下找到tomcat-users.xml文件.该文件路径为[tomcat安装根目录]/conf/ 修改文件内容,增加下列内容:(一般配 ...
- 解决waitfor()阻塞问题
运行代码执行exe,shell这样的程序或脚本再java中需: (1) 使用Runtime的exec()方法 (2) 使用ProcessBuilder的start()方法 Runtime和Proces ...
- LeetCode OJ-- Populating Next Right Pointers in Each Node
https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node/ 二叉树遍历的变种:树的节点多了个next指针 ...
- Oracle 表分区partition(http://love-flying-snow.iteye.com/blog/573303)
http://www.jb51.net/article/44959.htm Oracle表分区分为四种:范围分区,散列分区,列表分区和复合分区. 一:范围分区 就是根据数据库表中某一字段的值的范围来划 ...
- 10.1综合强化刷题 Day7
noip提高组模拟赛 ...