题目描述:

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的更多相关文章

  1. [LeetCode] 83. Remove Duplicates from Sorted List 移除有序链表中的重复项

    Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1 ...

  2. LeetCode 83. Remove Duplicates from Sorted List (从有序链表中去除重复项)

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  3. 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 ...

  4. [LeetCode] 83. Remove Duplicates from Sorted List ☆(从有序链表中删除重复项)

    描述 Given a sorted linked list, delete all duplicates such that each element appear only once. Exampl ...

  5. [leetcode]83. Remove Duplicates from Sorted List有序链表去重

    Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1 ...

  6. [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 ...

  7. [LeetCode]83. Remove Duplicates from Sorted List(排序链表去重)

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  8. leetCode 83.Remove Duplicates from Sorted List(删除排序链表的反复) 解题思路和方法

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  9. Leetcode 83 Remove Duplicates from Sorted List 链表

    就是将链表中的重复元素去除 我的方法很简单就是如果链表的前后元素相同的话,将后一个元素删除 /** * Definition for singly-linked list. * struct List ...

随机推荐

  1. Leetcode#78 Subsets

    原题地址 有两种方法: 1. 对于序列S,其子集可以对应为一个二进制数,每一位对应集合中的某个数字,0代表不选,1代表选,比如S={1,2,3},则子集合就是3bit的所有二进制数. 所以,照着二进制 ...

  2. 有关javascript中的JSON.parse和JSON.stringify的使用一二

    有没有想过,当我们的大后台只是扮演一个数据库的角色,json在前后台的数据交换中扮演极其重要的角色时,作为依托node的前端开发,其实相当多的时间都是在处理数据,准确地说就是在处理逻辑和数据(这周实习 ...

  3. 用户 IIS APPPOOL\DefaultAppPool 登录失败的解决方法

    解决方案  1.先查看你的网站使用的是哪个应用程序池 2.设置应用程序池的选项 3.标识选项 选LocalSystem或NetworkService就可以解决问题

  4. [nowCoder] 两个不等长数组求第K大数

    给定两个有序数组arr1和arr2,在给定一个整数k,返回两个数组的所有数中第K小的数.例如:arr1 = {1,2,3,4,5};arr2 = {3,4,5};K = 1;因为1为所有数中最小的,所 ...

  5. .bash_profile备份

    # ~/.bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User sp ...

  6. tomcat 解析(四)-处理http请求过程

    声明:源码版本为Tomcat 6.0.35 前面的文章中介绍了Tomcat初始化的过程,本文将会介绍Tomcat对HTTP请求的处理的整体流程,更细节的. 在上一篇文章中,介绍到JIoEndpoint ...

  7. Android Activity初探

    原地址:Android Activity初探 Activity是一个应用中的组件,它为用户提供一个可视的界面,方便用户操作,比如说拔打电话.照相.发邮件或者是浏览地图等.每个activity会提供一个 ...

  8. 【c++基础】const、const指针、const引用

    一.const常量 声明时必须同时初始化(和“引用”一样) 二.const指针 三.const引用 引用本身和引用的对象都是const对象,可以用字面值来赋给const引用(普通引用则不行) ; co ...

  9. hdu 4277 USACO ORZ

    没什么好方法,只能用dfs了. 代码如下: #include<iostream> #include<cstring> #include<cstdio> #inclu ...

  10. StringBuffer的用法

    StringBuffer类和String一样,也用来代表字符串,只是由于StringBuffer的内部实现方式和String不同,所以StringBuffer在进行字符串处理时,不生成新的对象,在内存 ...