LeetCode算法题-Remove Duplicates from Sorted List

这是悦乐书的第160次更新,第162篇原创

01 前情回顾

昨晚的爬楼梯算法题,有位朋友提了个思路,使用动态规划算法。介于篇幅问题,这里不细说动态规划算法,以后会在数据机构和算法的理论知识里细说。

昨晚的三个解法中,根据测试数据和结果,第三种解法是最优的,但是还能不能更进一步呢?经过推导,我们得知当n大于等于3的时候,f(n) = f(n-1)+f(n-2),也就是说我们只需要得到n的前面两位的结果即可,对此我们使用了数组,将每个值都存起来了,最后取出数组中的最后一位元素。那么是否可以将数组也省掉,降低空间复杂度呢?答案是可以的,我们可以使用临时变量存值。

public int climbStairs4(int n) {
    if (n == 0 || n == 1) {
        return 1;
    }
    int a = 1;
    int b = 1;
    int temp = 0;
    for (int i=2; i<= n; i++) {
        temp = a + b;
        a = b;
        b = temp;
    }
    return temp;
}

02 看题和准备

今天介绍的是LeetCode算法题中Easy级别的第19题(顺位题号是83)。给定已排序的链接列表,删除所有重复项,使每个元素只出现一次。例如:

输入:1-> 1-> 2

输出:1-> 2

输入:1-> 1-> 2-> 3-> 3

输出:1-> 2-> 3

本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试。

03 第一种解法

特殊情况一:当head为空时,直接返回空。

特殊情况二:当head没有下一个节点时,肯定是没有重复节点值的,直接返回head本身。

正常情况:既然要判断节点值是否重复,免不了循环。另外,还要考虑是否要建一个新的链表来连接最后去重的节点值。

首先,获取head的下一个节点值,判断head.val和head.next.val是否相等,如果相等,此时head节点的下一个节点应该跳到head.next.next这里,如果不相等,那么head节点的下一个节点只用跳到head.next即可,接着进入下一次判断。由此,我们可以直接使用head本身,只是需要重新改变它的节点连接对象,也就是它改变原本每个节点的引用,而不需要额外使用新的空间来存储去重后的链表。

public ListNode deleteDuplicates(ListNode head) {
    if (head == null || head.next == null) {
        return head;
    }
    ListNode first = head;
    ListNode second = first.next;
    while (second != null) {
        if (first.val == second.val) {
            second = second.next;
            first.next = second;
        } else {
            first = first.next;
            second = second.next;
        }
    }
    return head;
}

04 小结

去除链表的重复节点值和之前去除数组重复元素的题有点类似,关于链表的简单介绍在合并链表的题有讲过,可以找找之前的文章看看。

以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!

原文地址:https://www.cnblogs.com/xiaochuan94/p/9899813.html

时间: 2025-01-15 03:33:22

LeetCode算法题-Remove Duplicates from Sorted List的相关文章

乘风破浪:LeetCode真题_026_Remove Duplicates from Sorted Array

乘风破浪:LeetCode真题_026_Remove Duplicates from Sorted Array 一.前言     我们这次的实验是去除重复的有序数组元素,有大体两种算法. 二.Remove Duplicates from Sorted Array 2.1 问题      题目大意理解,就是对数组进行元素去重,然后返回去处重复之后的长度,无论我们对数组做了什么的修改,都没有关系的,只要保证再返回的长度之内的数组正确性即可.因为最后是根据长度来遍历的,因此我们不用担心. 2.2 分析

Leetcode 线性表 Remove Duplicates from Sorted Array II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Duplicates from Sorted Array II Total Accepted: 10649 Total Submissions: 35325 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorte

Leetcode 线性表 Remove Duplicates from Sorted Array

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Duplicates from Sorted Array Total Accepted: 14789 Total Submissions: 46601 Given a sorted array, remove the duplicates in place such that each element appear only once and return the new l

Leetcode 线性表 Remove Duplicates from Sorted List

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Duplicates from Sorted List Total Accepted: 14961 Total Submissions: 43772 Given a sorted linked list, delete all duplicates such that each element appear only once. For example, Given 1->1

leetcode_80题——Remove Duplicates from Sorted Array II(两个指针)

Remove Duplicates from Sorted Array II Total Accepted: 38480 Total Submissions: 125235My Submissions Question Solution Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,

【一天一道LeetCode】#83. Remove Duplicates from Sorted List

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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-&

leetcode 题解:Remove Duplicates from Sorted Array II(已排序数组去三次及以上重复元素)

题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 说明: 1)设个标志可实现 实现: 1 class Solution { 2 public

leetcode 题解:Remove Duplicates from Sorted Array(已排序数组去重)

题目: Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example,Given input array A

Leetcode 线性表 Remove Duplicates from Sorted List II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Duplicates from Sorted List II Total Accepted: 10702 Total Submissions: 43714 Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from th