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 ...
随机推荐
- PAT 1017 Queueing at Bank (模拟)
1017. Queueing at Bank (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Supp ...
- centos7 docker镜像加速器配置
CentOS的配置方式略微复杂,需要先将默认的配置文件复制出来 /lib/systemd/system/docker.service -> /etc/systemd/system/docker. ...
- centos6.5关闭防火墙命令
1.永久性生效,重启后不会复原 开启: chkconfig iptables on 关闭: chkconfig iptables off 2.即时生效,重启后复原 开启: service iptabl ...
- Neutron相关资料链接
1.OpenStack Neturon 官方文档: https://docs.openstack.org/mitaka/networking-guide/ 2.Neturon理解系列文章: http: ...
- Java加密技术(四)非对称加密算法RSA
RSA 这样的算法1978年就出现了.它是第一个既能用于数据加密也能用于数字签名的算法.它易于理解和操作.也非常流行.算法的名字以发明者的名字命名:Ron Rivest, AdiShamir ...
- StrobeMediaPlayback的Javascript桥接
StrobeMediaPlayback是Adobe官方出的流媒体播放器,支持RTMP协议,在项目中运用到了,却在网上怎么都找不到相关资料,可以说是寥寥无几. 无奈之下,稍微看了点源代码,对播放器与JS ...
- Java 实现文件随机读写-RandomAccessFile
现有如下的一个需求,向已存在1G数据的txt文本里末尾追加一行文字,内容如下“Lucene是一款非常优秀的全文检索库”.可能大多数朋友会觉得这个需求很easy,说实话,确实easy,然后XXX君开始实 ...
- iOS学习之数据持久化详解
前言 持久存储是一种非易失性存储,在重启设备时也不会丢失数据.Cocoa框架提供了几种数据持久化机制: 1)属性列表: 2)对象归档: 3)iOS的嵌入式关系数据库SQLite3: 4)Core Da ...
- beego——事务处理和命令模式
1.事务处理 ORM 可以简单的进行事务操作. o := NewOrm() err := o.Begin() // 事务处理过程 ... ... // 此过程中的所有使用 o Ormer 对象的查询都 ...
- 初识JS 基本语法.基本运算符
JavaScript概述 JavaScript的历史 1992年Nombas开发出C-minus-minus(C--)的嵌入式脚本语言(最初绑定在CEnvi软件中).后将其改名ScriptEase.( ...