首页 > 其他分享 >力扣---面试题 02.01. 移除重复节点

力扣---面试题 02.01. 移除重复节点

时间:2023-04-03 16:46:34浏览次数:29  
标签:面试题 set ListNode val head next --- 移除 null

编写代码,移除未排序链表中的重复节点。保留最开始出现的节点。

示例 1:

输入:[1, 2, 3, 3, 2, 1]
输出:[1, 2, 3]
示例 2:

输入:[1, 1, 1, 1, 2]
输出:[1, 2]
提示:

链表长度在 [0, 20000] 范围内。
链表元素在 [0, 20000] 范围内。
进阶:

如果不得使用临时缓冲区,该怎么解决?

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/remove-duplicate-node-lcci
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。


 

先写普通的,即用一个Set来存储遍历过的节点值,然后判断节点是否包含在里面,如果是,那么通过next移除下一个节点。

最近递归用的越来越顺手了。直接用递归写的,但写完后发现其实按照我的思路,也许不写成递归会更简洁点。

非进阶递归:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode removeDuplicateNodes(ListNode head) {
        if (head == null) {
            return null;
        }
        Set<Integer> set = new HashSet<>();
        delete(head, set);
        return head;
    }
    private void delete(ListNode head, Set<Integer> set) {
        if (head == null || head.next == null) {
            return;
        }
        set.add(head.val);
        if (set.contains(head.next.val)) {
            head.next = head.next.next;
            delete(head, set);
        } else {
            delete(head.next, set);
        }
    }
}

不使用额外缓冲区(实际上就是时间换空间,将set减掉的一层时间复杂度还原而已。虽然,但是,这个进阶还是很值得吐槽的)。

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode removeDuplicateNodes(ListNode head) {
        if (head == null) {
            return null;
        }
        delete(head);
        return head;
    }
    private void delete(ListNode head) {
        if (head == null || head.next == null) {
            return;
        }
        ListNode node = head;
        // 遍历,然后删除重复的节点。
        while (node.next != null) {
            if (node.next.val == head.val) {
                node.next = node.next.next;
            } else {
                node = node.next;
            }
        }
        delete(head.next);
    }
}

 

标签:面试题,set,ListNode,val,head,next,---,移除,null
From: https://www.cnblogs.com/allWu/p/17283496.html

相关文章

  • THM-密码攻击(Password Attacks)
    密码攻击技术密码攻击技术在这个房间里,我们将讨论可用于执行密码攻击的技术。我们将介绍各种技术,例如字典、蛮力、规则库和猜测攻击。上述所有技术都被视为主动“在线”攻击,其中攻击者需要与目标机器通信以获取密码,以便获得对机器的未授权访问。密码破解与密码猜测本节从网......
  • 26-springboot-thymeleaf字符串拼接-常量-符号
    Thymeleaf字符串拼接一种是字符串拼接:<spanth:text="'当前是第'+${sex}+'页,共'+${sex}+'页'"></span>另一种更简洁的方式,使用“|”减少了字符串的拼接:<spanth:text="|当前是第${sex}页,共${sex}页|"></span>Thymeleaf可直接使用的常量和符号1、所有......
  • 27-springboot-thymeleaf内置对象
    1、内置web对象thymaleaf内置的web对象,可以直接在模板中使用,这些对象由#号开头:#request:相当于HttpServletRequest对象,这是Thymeleaf3.x版本,若是Thymeleaf2.x版本使用#httpServletRequest;${#request.getContextPath()}${#request.getAttribute("phone")}#session:相当于H......
  • 【Struts框架】第一节Action-模块包含和defaultAction
    1.模块包含:struts.xml:里面可以这么写<includefile="login.xml"></include>说明在struts.xml包含了一个login.xml文件login.xml:<?xmlversion="1.0"encoding="GBK"?><!DOCTYPEstrutsPUBLIC"-//apacheSoftwareFound......
  • MyBatisPlus---delete删除操作的三种方法
    一、根据id删除1234567891011@Testpublic void deleteById(){    int rows=userMapper.deleteById(1351456313578713090L);    System.out.println("删除条数:" +rows);} @Testpublic void deleteByBatchIds(){    int row......
  • 数据分析-字词云
    数据预处理#代码12-1评论去重的代码importpandasaspdimportre#正则匹配importjieba.possegaspsgimportnumpyasnp#去重,去除完全重复的数据reviews=pd.read_csv("D:/人工智能&软件工程/数据挖掘与分析/dem......
  • 加载更多 - 监听div的滚动scroll
    前言:某些情况下,在展示列表数据时,为了实现性能优化及用户更好的体验,可以先展示十几条数据,然后边滑动边加载更多,可以减少服务器压力及页面渲染时间。varpageNum=1;//页数vardomHeight=$(".listBox").height()*4;vardom=document.getElementById('list');dom.addEventList......
  • 24-springboot-thymeleaf的表达式
    1.添加热部署,为了测试不用频繁重启<!--热部署插件--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional><!--防止将该依赖传递到其他模块中--></depen......
  • 25-springboot-thymeleaf的常见属性
    th:action<formid="login"th:action="@{/login}">......</form>th:method<formid="login"th:action="@{/login}"th:method="post">......</form>th:href<a class="login"......
  • 函数式编程-高阶函数
    函数本身也可以赋值给变量,即:变量可以指向函数  那么函数名是什么呢?函数名其实是指向函数的变量!对于abs()这个函数,完全可以把函数名abs看成变量,它指向一个可以计算绝对值的函数! 既然变量可以指向函数,函数的参数能接收变量,那么一个函数就可以接收另一个函数作为参数,这种函......