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 ...
随机推荐
- iOS 修改状态栏颜色
1.首先,苹果把UIViewControllerBasedStatusBarAppearance默认的值设为YES,是有他的道理的,新系统下,苹果希望我们的viewcontroller去控制statu ...
- 160622、详解JavaScript变量提升
变量在程序中随处可见.它们是一些始终在相互影响,相互作用的的数据和逻辑.正是这些互动使应用程序活了起来. 在JavaScript中使用变量很重要的一方面就是变量的提升 —— 它决定了一个变量何时可以被 ...
- HDU 4059 The Boss on Mars(容斥原理)
The Boss on Mars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- golang 开发过程中的坑
1. chan数据读取写入 正常情况下chan读取写入都没有问题,但是如果chan关闭之后会出现问题 所以读取chan数据的时候需要增加chan是否关闭的判断 c := make(chan ) v, ...
- TADDConnetion组件,TADOQuery
一.TADDConnetion 二.TADOQuery 1.RecNo:从1开始 当前记录行数;ADOQuery1.RecNo 选择后一行数据集内容:ADOQuery1.RecNo:=ADOQuery ...
- c# WinForm英雄联盟挂机源码及实现原理
主要功能:全自动化英雄联盟挂机,游戏中会在原地放技能保持不掉线状态,游戏结束自动重新开始,自动选择英雄,可以晚上挂机刷人机: 缺陷:没怎么完善,如果掉线或者游戏崩溃网络断了软件会自动停止操作,使用时间 ...
- jQuery实现图片预览
摘自:http://www.cnblogs.com/leejersey/p/3660202.html JS代码: /* *名称:图片上传本地预览插件 v1.1 *作者:周祥 *时间:2013年11月2 ...
- linkText()的用法
1.linkText()常用于定位链接,以谷歌的gmail为例: WebElement gmailLink = driver.findElement(By.linkText("Gmail&q ...
- 10.Query an Array-官方文档摘录
1.插入 db.inventory.insertMany([ { item: "journal", qty: 25, tags: ["blank", " ...
- flannel源码分析---backend为vxlan
// backend/vxlan/vxlan.go func (be *VXLANBackend) RegisterNetwork(ctx context.Context, network strin ...