LeetCode OJ 82. 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
.
相对于Remove Duplicates from Sorted List这个问题,这个问题是要把重复出现过的所有元素全部删除。我的想法是找出每一个数字段的起点和终点,如果这个数据段出现了重复元素,就把这个数据段整体删除。
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head==null || head.next==null) return head;
ListNode thead = new ListNode(-1);
ListNode begin = thead;
ListNode end = head;
thead.next = head; int cur = head.val;
while(end.next!=null){
if(end.next.val==cur){
end = end.next;
}
else{
if(begin.next==end){
begin = end;
end = end.next;
}
else{
begin.next = end.next;
end = begin.next;
}
cur = end.val;
}
}
if(begin.next==end) return thead.next;
begin.next = null;
return thead.next;
}
}
LeetCode OJ 82. Remove Duplicates from Sorted List II的更多相关文章
- 【LeetCode】82. Remove Duplicates from Sorted List II 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/remove-du ...
- LeetCode OJ 80. Remove Duplicates from Sorted Array II
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- LeetCode OJ:Remove Duplicates from Sorted Array II(移除数组中的重复元素II)
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- 【一天一道LeetCode】#82. Remove Duplicates from Sorted List II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【Leetcode】82. Remove Duplicates from Sorted List II
Question: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only dis ...
- LeetCode OJ:Remove Duplicates from Sorted List II(链表去重II)
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- leetcode 203. Remove Linked List Elements 、83. Remove Duplicates from Sorted List 、82. Remove Duplicates from Sorted List II(剑指offer57 删除链表中重复的结点)
203题是在链表中删除一个固定的值,83题是在链表中删除重复的数值,但要保留一个:82也是删除重复的数值,但重复的都删除,不保留. 比如[1.2.2.3],83题要求的结果是[1.2.3],82题要求 ...
- 82. Remove Duplicates from Sorted List II && i
题目 83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such tha ...
- 82. Remove Duplicates from Sorted List II【Medium】
82. Remove Duplicates from Sorted List II[Medium] Given a sorted linked list, delete all nodes that ...
随机推荐
- Hibernate5--课程笔记5
关联关系映射: 关联关系,是使用最多的一种关系,非常重要.在内存中反映为实体关系,映射到DB中为主外键关系.实体间的关联,即对外键的维护.关联关系的发生,即对外键数据的改变. 外键:外面的主键,即,使 ...
- 外部系统集成BIEE
1.外部系统集成BIEE 隐藏工具栏和仪表盘右上角的菜单 2.BIEE 11g 嵌入Iframe InIFrameRenderingMode有三种取值,分别是prohibit.sameDomainOn ...
- 跨域资源共享(Cross-Origin Resource Sharing)
目前中文方面的资料还比较少,能搜索到的那仅有的几篇相关介绍,也几乎是雷同的,其中C#方面的更是少之又少. XMLHttpRequest接口是Ajax的根本,而Ajax考虑到安全性的问题,是禁止跨域访问 ...
- 学习前端前必知的——HTTP协议详解
前端人士必备的知识点,无论你是否有经验,看了此文绝对有收获 此文针对前端爱好者,前端求职者(话说面试时很容易考到哦) 原文参考博客园http://kb.cnblogs.com/page/130970/ ...
- java 用双向链表实现SJBLinkedList
public class SJBLinkedList{ private Node first; private Node last; private int size; public int size ...
- iOS开发富文本
NSMutableAttributedString * attributedStr = [[NSMutableAttributedString alloc] initWithString:@" ...
- 邮件发送 java
package com.sun.mail; import java.io.File;import java.io.IOException;import java.io.UnsupportedEncod ...
- HTTP Request GET, HEAD, POST, PUT, DELETE, OPTIONS, TRACE Methods
注:本文为个人学习摘录,原文地址为:http://javaeedevelop.iteye.com/blog/1725299 An HTTP request is a class consisting ...
- Spring自动扫描
需要在Springde 配置文件中加入 <context:component-scan base-package="com.annoation"> base-packa ...
- openwrt生成备份文件
生成备份文件时所使用的脚本中调用的命令为sysgrade local image_tmp = "/tmp/firmware.img" local backup_cmd = & ...