【原创】leetCodeOj ---Remove Duplicates from Sorted List II 解题报告
明日深圳行,心情紧张,写博文压压惊 囧
-------------------------------------
原题地址:
https://oj.leetcode.com/problems/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
.
方法:
递归做非常直观。
判断当前节点的值是否等于下一个节点的值,若不等,直接递归调用下一个节点;
若相等,则找到下一个不和当前节点相等的节点,并将其更新为当前节点,同时递归调用该节点。
全部代码:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
if (head == null)
return null;
if (head.next == null)
return head;
if (head.next.val == head.val)
{
while (head.next != null && head.next.val == head.val)
{
head = head.next;
}
head = deleteDuplicates(head.next);
}
else
{
head.next = deleteDuplicates(head.next);
}
return head;
}
}
【原创】leetCodeOj ---Remove Duplicates from Sorted List II 解题报告的更多相关文章
- Leetcode: Remove Duplicates from Sorted List II 解题报告
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
- 【LeetCode】82. Remove Duplicates from Sorted List II 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/remove-du ...
- 【LeetCode】80. Remove Duplicates from Sorted Array II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- Remove Duplicates from Sorted List II
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
- 48. Remove Duplicates from Sorted List && Remove Duplicates from Sorted List II
Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such that each ...
- 【LeetCode练习题】Remove Duplicates from Sorted List II
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
- Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II
以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置 一:Remove Element Given an array and a value, remove a ...
随机推荐
- poj 1611 The Suspects(并查集)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21598 Accepted: 10461 De ...
- hdu 5115 Dire Wolf
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5115 题目分类:区间dp 题意:有n只狼,每只狼有两种属性,一种攻击力一种附加值,我们没杀一只狼,那么 ...
- [Windows Phone]模仿魔兽3技能按钮SkillButton
简介: 模仿魔兽3技能按钮,带CD效果.使用的时候可以当做普通按钮使用,同时也支持Binding. 音效紧耦合在控件内部,因为控件本身目的就是模拟魔兽3的技能按钮,所以不考虑音效的扩展. Demo结构 ...
- [Python] heapq简介
[Python] heapq简介 « Lonely Coder [Python] heapq简介 judezhan 发布于 2012 年 8 月 8 日 暂无评论 发表评论 假设你需要维护一个列表,这 ...
- heapq
heapq-Guest-ChinaUnix博客 假设你需要维护一个列表,这个列表不断有新的元素加入,你需要在任何时候很方便的得到列表中的最大(小)值,因此要求列表始终处于排序完毕状态,怎么办呢 最简单 ...
- Linux Shell 之 我的第一个Shell程序
这里我首先会介绍一个Shell是什么,再介绍我的第一个Shell程序和从中总结的经验. 一.Shell是什么 在说我的这个Shell程序之前,还是先跟大家说说什么是Shell吧,相信Shell这个 ...
- android4.3环境搭建
方案一: 首先android环境搭建有如下几个东西是必须准备的: 1. Eclipse (下载地址:http://www.eclipse.org/downloads/,建议至少3.4及以上版本) 2 ...
- hdu1330(递推)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1330 分析:经典问题,n 块相同的木板重叠,最多能够伸出桌面多远 对于n张卡片的最佳摆法,我们只需要在 ...
- UVA 11100 The Trip, 2007 贪心(输出比较奇葩)
题意:给出n个包的大小,规定一个大包能装一个小包,问最少能装成几个包. 只要排序,然后取连续出现次数最多的数的那个次数.输出注意需要等距输出. 代码: /* * Author: illuz <i ...
- Gap year | 最好金龟换酒
Gap year | 最好金龟换酒 Gap year Posted on February 8, 2009 by 真 后.90后相比,说是虽然形成背景不同,但有很多特征相似,比如离经叛道,比如信仰缺失 ...