Java [Leetcode 83]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.
代码如下:
代码一,正常解法:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head == null)
return null;
ListNode cur = head.next;
ListNode pre = head;
while(cur != null){
if(cur.val == pre.val){
if(cur.next != null){
cur.val = cur.next.val;
cur.next = cur.next.next;
}else{
pre.next = null;
cur = null;
}
} else {
pre = cur;
cur = cur.next;
}
}
return head;
}
}
思路二,递归解法:
public ListNode deleteDuplicates(ListNode head) {
if(head == null || head.next == null)return head;
head.next = deleteDuplicates(head.next);
return head.val == head.next.val ? head.next : head;
}
Java [Leetcode 83]Remove Duplicates from Sorted List的更多相关文章
- [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 ...
- Java [leetcode 26]Remove Duplicates from Sorted Array
题目描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ...
- [LeetCode] 83. Remove Duplicates from Sorted List ☆(从有序链表中删除重复项)
描述 Given a sorted linked list, delete all duplicates such that each element appear only once. Exampl ...
- [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_Easy tag: Linked 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 ...
- leetCode 83.Remove Duplicates from Sorted List(删除排序链表的反复) 解题思路和方法
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- Leetcode 83 Remove Duplicates from Sorted List 链表
就是将链表中的重复元素去除 我的方法很简单就是如果链表的前后元素相同的话,将后一个元素删除 /** * Definition for singly-linked list. * struct List ...
随机推荐
- httpClient 入门实例
import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.Unsu ...
- Unity3D Script Execution Order ——Question
我 知道 Monobehaviour 上的 那些 event functions 是 在主线程 中 按 顺序调用的.这点从Manual/ExecutionOrder.html 上的 一张图就可以看出来 ...
- 管道命令xargs
在研究hadoop的过程中,遇到一个小难题. 问题描述:我需要将文件夹A下的文件拷贝到文件夹B中,但是以hadoop开头的文件不要拷贝. 问题解决:ls A|grep -v hadoop|xargs ...
- POJ 1734
#include<iostream> #include<stdio.h> #define MAXN 105 #define inf 123456789 using namesp ...
- DVB系统几种传输方式
卫星 (DVB-S 及 DVB-S2)有线 (DVB-C)地面无线 (DVB-T)手持地面无线 (DVB-H)
- YWE复习
1.怎样使用C#的[状态栏][StatusStrip]控件 http://jingyan.baidu.com/article/851fbc37e7004e3e1f15ab3d.html 2.toolt ...
- Linux内核的同步机制
本文详细的介绍了Linux内核中的同步机制:原子操作.信号量.读写信号量和自旋锁的API,使用要求以及一些典型示例 一.引言 在现代操作系统里,同一时间可能有多个内核执行流在执行,因此内核其实象多进程 ...
- TCL语言笔记:TCL中的控制结构命令
一.引言 控制结构允许程序根据不同的状态.条件和参数来选择不同的处理和执行路径,从而使代码具有更强的灵活性.健壮性和可读性. Tcl 提供了 if.if/else.if/elseif.foreach. ...
- 百度地图API简单使用
百度地图API是由JavaScript语言编写的,在使用之前需要将API引用到页面中: 现在新版本的需要密钥,下面用的是旧版的 <script src="http://api.map ...
- 机器人学 —— 飞行机器人(Introduction)
UPNN课程 aerial robotics 教授: VJ Kummer 1.四旋翼飞行器的控制对象是各个旋翼对应的电机 2.飞行器的能源主要消耗于hovering. 3.飞行器在设计时需要考虑各个 ...