【原创】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 ...
随机推荐
- Codeforces Round #309 (Div. 2) C
题意: 就是给出总共同拥有k种颜色.每种颜色有ki种,排列必须满足第i+1种的最后一种颜色必须在第i种最后一种颜色的后面,其它颜色任意.总共同拥有多少种排列点的方法. 分析: 如果d[i]表示前i种的 ...
- CSS 文本框里添加按钮的实现
有很多人做界面会经常发现设计师设计出这样的界面: 咋一看是一个文本框里加了一个按钮,经过谷歌之后,未发现在文本框里可以添加按钮. 但可以通过div来实现它. 我的做法是先做一个大小的div,然后用带里 ...
- 利用未公开API获取终端会话闲置时间(Idle Time)和登入时间(Logon Time)
利用未公开API获取终端会话闲置时间(Idle Time)和登入时间(Logon Time)作者:Tuuzed(土仔) 发表于:2008年3月3日23:12:38 版权声明:可以任意转载,转载时请 ...
- sql,nosql
1. 关系型数据库 关系型数据库,是指采用了关系模型来组织数据的数据库. 关系模型是在1970年由IBM的研究员E.F.Codd博士首先提出的,在之后的几十年中,关系模型的概念得到了充分的发展并逐渐成 ...
- QML性能
1) Limit JavaScript a) inline JavaScript: 内联的JavaScript方法; 1. 将js方法放置在Element内部; 2. 尝试将语句写在一行内; ...
- 世界gis相关的资源网站分类整理
********************首先介绍个新颖的GIS论坛——GIS520论坛******************** GIS520论坛(共享地信学习资源的专业论坛) www.gis520.c ...
- java socket 的参数选项解读(转)
java socket中有很多参数可以选择,这篇博客的目的是沉淀出这些参数的语义和用法,供自己以后查阅. 1.java socket参数选项总览 在JDK1.6中有如下参数选项: 1 public f ...
- SSH WebShell: SSH在线WEB管理器安装教程 - VPS管理百科
SSH WebShell: SSH在线WEB管理器安装教程 - VPS管理百科 SSH WebShell: SSH在线WEB管理器安装教程 本站原创 [基于 署名-非商业使用-相同方式分享 2.5 协 ...
- POJ 3458 Colour Sequence(简单题)
[题意简述]:事实上题意我也没有特别看懂.可是依据它少许的题目描写叙述加上给的例子.就大胆的做了例如以下的推測: 就是说,如今给出一串字符s.然后紧接着给出可见的字符串visible还有隐藏的字符串h ...
- Phalcon之 表单(Forms)
Phalcon中提供了 Phalcon\Forms组件以方便开发人员创建和维护应用中的表单. 以下的样例中展示了主要的用法: <?php use Phalcon\Forms\Form, Phal ...