[LeetCode][Java] 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->2->3->3->4->4->5 。返回1->2->5.
给定1->1->1->2->3 ,返回2->3.
算法分析:
设置前后双指针。后指针遇到反复元素就一直遍历直到反复的结尾,之后前指针指向后指针,这样就略过全部的反复元素。
AC代码:
<span style="font-family:Microsoft YaHei;font-size:12px;">public class Solution
{
public ListNode deleteDuplicates( ListNode head)
{
ListNode pre;
ListNode cur;
ListNode newhead = new ListNode(0);
newhead.next=head;
if(head==null||head.next==null)
return head;
pre=newhead;
cur=head;
while(cur.next!=null)
{
if(cur.next.val==cur.val)//处理头几个元素同样的样例 如1 1 1 2 3 4
{
while(cur.next.val==cur.val)
{
cur=cur.next;
if(cur.next==null)//处理末尾几个元素同样的样例 1 2 3 4 5 5 5
break;
}
pre.next=cur.next;
//pre=pre.next;
cur=cur.next;
if(cur==null)
break;
}
else//处理头几个元素不同样的样例 1 2 3 4 5
{
pre=pre.next;
cur=cur.next;
} }
return newhead.next;
}
}</span>
[LeetCode][Java] Remove Duplicates from Sorted List II的更多相关文章
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项 II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)
https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...
- [leetcode] 80. Remove Duplicates from Sorted Array II (Medium)
排序数组去重题,保留重复两个次数以内的元素,不申请新的空间. 解法一: 因为已经排好序,所以出现重复的话只能是连续着,所以利用个变量存储出现次数,借此判断. Runtime: 20 ms, faste ...
- LeetCode OJ Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项之二
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
随机推荐
- 如何使用postman访问网站
1.输入Request URL2.选择Request Method3.输入需要的Request Headers注意:一般token会在Headers中
- Angular JavaScript内存溢出问题 (FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory)
方法一和方法二参考:https://www.cnblogs.com/liugang-vip/p/6857595.html 方法一:my-project/node_modules/.bin 下增大内存( ...
- mongdb查询数据并且返回数据条数
var totall; var a = db.db("Magiccat").collection("jishi_content").find().count({ ...
- 10MongoDB
一. 介绍MongoDB 1. NoSQL 1)“NoSQL”⼀词最早于1998年被⽤于⼀个轻量级的关系数据库的名字 随着web2.0的快速发展, NoSQL概念在2009年被提了出来 2)NoSQ ...
- 如何用纯 CSS 创作一个渐变色动画边框
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/odpRKX 可交互视频教 ...
- Django 多个字段关联同一外键
# -*- coding: utf-8 -*- """ Tencent is pleased to support the open source community b ...
- 03001_Http协议
1.什么是Http协议 (1)HTTP,超文本传输协议(HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议: (2)所有的WWW文件都必须遵守这个标准: (3 ...
- Java 新手学习日记一
Java 基础知识点掌握: 数据类型 变量就是申请内存来存储值.也就是说,当创建变量的时候,需要在内存中申请空间.内存管理系统根据变量的类型为变量分配存储空间,分配的空间只能用来储存该类型数据. 因此 ...
- HDU 4821 字符串hash
题目大意: 希望找到连续的长为m*l的子串,使得m个l长的子串每一个都不一样,问能找到多少个这样的子串 简单的字符串hash,提前预处理出每一个长度为l的字符串的hash值 #include < ...
- 矩形周长(codevs 2149)
题目描述 Description N(N<5000) 张矩形的海报,照片和其他同样形状的图片贴在墙上.它们的边都是垂直的或水平的.每个矩形可以部分或者全部覆盖其他矩形.所有的矩形组成的集合的轮廓 ...