Remove Duplicates from Sorted List ,除去链表中相邻的重复元素
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
.
public ListNode deleteDuplicates(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode temp = head;
while (temp.next != null) {
if (temp.next.val == temp.val) {
temp.next = temp.next.next;
}
else
{
temp = temp.next;
}
}
return head;
}
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
.
算法分析:区别问题1,这个是把所有重复元素都删掉,一个不留,所有要用两重while去重,上面一题去重时只需要一重循环。
public ListNode deleteDuplicates2(ListNode head)
{
if(head == null || head.next == null)
{
return head;
}
ListNode pre = new ListNode(0);//记录头结点,因为开头如果有重复元素,head将改变
pre.next = head;
ListNode temp = pre;
while (temp.next != null && temp.next.next != null) {
if(temp.next.val == temp.next.next.val)
{
int dup = temp.next.val;
while(temp.next != null && temp.next.val == dup)//去掉所有重复元素
{
temp.next = temp.next.next;
}
}
else
{
temp = temp.next;
}
}
return pre.next;
}
Remove Duplicates from Sorted List ,除去链表中相邻的重复元素的更多相关文章
- Remove Duplicates from Sorted List 去除链表中重复值节点
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- 083 Remove Duplicates from Sorted List 有序链表中删除重复的结点
给定一个排序链表,删除所有重复的元素使得每个元素只留下一个.案例:给定 1->1->2,返回 1->2给定 1->1->2->3->3,返回 1->2- ...
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- [LeetCode] 26. Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...
- [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]83. Remove Duplicates from Sorted List(排序链表去重)
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- 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 II 链表
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)
[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...
随机推荐
- AEcs6破解版下载
下载地址 http://pan.baidu.com/share/link?shareid=79184520&uk=1795677788 点击下载
- Openstack深入了解虚拟机
续Openstack虚拟机创建流程 在控制节点也安装一个计算服务也变成一个计算节点 yum -y install openstack-nova-compute 启动并且设置开机启动 systemctl ...
- python之django直接执行sql语句
python之django直接执行sql语句 sql = 'select * from stu' info = 模型类.objects.raw(sql)
- mysql中排序
排序(默认:asc升序; desc降序 如:根据成绩从高到低排序 select * from stu_info order by mark desc; 根据成绩从低到高排序 select * from ...
- js实现查询关键词,使其高亮
今天做了一个功能:在页面上查询关键,使其高亮显示,实现代码如下: css: <style type="text/css"> .highlight { backgroun ...
- 【转】windows 下 goprotobuf 的安装与使用
1. 安装 在网上看了很多教程,都提到要安装 protoc 与 protoc-gen-go,但通过尝试之后并不能正确安装 protoc,一下记录能够顺利安装 protoc 与 protoc-gen-g ...
- 10.php引用(&)详解及注意事项
<?php function &test() { static $b=0;//申明一个静态变量 $b=$b+1; echo $b; return $b; } $a=test();//这条 ...
- java工程编写manifest文件
如果需要一个可以单独运行的jar包“Runnable JAR file”,省事的方法是妥妥的选择打一个可运行的jar包“Runnable JAR file”.如此一来,就可以把程序运行所依赖的类.第三 ...
- Django-admin列表展示上传图片
1.在models.py文件中创建表ImageField类型字段 class user(models.Model): img = models.ImageField(upload_to='static ...
- JsonResponse对象浅析
JsonResponse JsonResponse 对象: class JsonResponse(data, encoder=DjangoJSONEncoder, safe=True, json_ ...