removeFromList
来源:互联网 发布:淘宝达人发布短视频 编辑:程序博客网 时间:2024/06/03 07:04
remove-duplicates-from-sorted-list:
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.
package leetcode;
/*一般会修改链表头的题目使用一个辅助的空指针。
* */
public class RemoveAllFromList {
public ListNode deleteAllDulicates(ListNode head){
if(head==null||head.next==null){
return head;
}
ListNode helper=new ListNode(0);
helper.next=head;
ListNode pre=helper;
ListNode cur=head;
while(cur!=null){
while(cur.next!=null&&pre.next.val==cur.next.val){
cur=cur.next;
}
if(pre.next==cur){
pre=pre.next; // 将pre指向上一个非重复元素;
}
else{
pre.next=cur.next; //将重复的元素删除
}
cur=cur.next;
}
return helper.next;
}
}
这是有序链表,重复的元素都在一起。
把前驱指针指向上一个不重复的元素中,如果找到不重复元素,则把前驱指针知道该元素,否则删除此元素。
开始没有注意题目是有序链表,想了好久
- removeFromList
- 阿里云函数计算
- 用Python从0实现贝叶斯分类器的机器学习教程
- apache压力测试工具ab详解
- 感恩节,Google 翻译给大家送礼物啦!
- Spring Boot理论篇--->编号001
- removeFromList
- SVN authorization failed
- 【备忘】Sqoop、Flume、Oozie、Hue大数据工具视频教程
- android.view.InflateException: Binary XML file line #22: Binary XML file line #22:
- 当Android Studio 3.0遇上Jenkins
- 机器学习决策树:提炼出分类器算法
- CentOS7上Python3环境搭建
- fastdfs
- HBuilder常用快捷键