算法记录lday3 LinkedList 链表移除 + 链表构建 + 链表反转reverse

news2024/10/6 16:30:32

今日任务

● 链表理论基础
● 203.移除链表元素
● 707.设计链表
● 206.反转链表

链表理论基础

建议:了解一下链接基础,以及链表和数组的区别

文章链接:https://programmercarl.com/%E9%93%BE%E8%A1%A8%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80.html

203.移除链表元素

建议: 本题最关键是要理解 虚拟头结点的使用技巧,这个对链表题目很重要。

题目链接/文章讲解/视频讲解::https://programmercarl.com/0203.%E7%A7%BB%E9%99%A4%E9%93%BE%E8%A1%A8%E5%85%83%E7%B4%A0.html

707.设计链表

建议: 这是一道考察 链表综合操作的题目,不算容易,可以练一练 使用虚拟头结点

题目链接/文章讲解/视频讲解:https://programmercarl.com/0707.%E8%AE%BE%E8%AE%A1%E9%93%BE%E8%A1%A8.html

206.反转链表

建议先看我的视频讲解,视频讲解中对 反转链表需要注意的点讲的很清晰了,看完之后大家的疑惑基本都解决了。

题目链接/文章讲解/视频讲解:https://programmercarl.com/0206.%E7%BF%BB%E8%BD%AC%E9%93%BE%E8%A1%A8.html

DeleteNode链表 leetcode

链接

https://leetcode.com/problems/remove-linked-list-elements/

题目表示

Given the head of a linked list and an integer val, remove all the nodes of the linked list that has Node.val == val, and return the new head.
在这里插入图片描述

思路

use one poiner cur to iterate thorough the whole linkedlist

whne the cur.val == val
remove this element

犯错点

处理头结点为目标值的情况,要用while循环处理
In the case of processing the head node as the target value,
it should be processed in a while loop.
处理完之后,判断head是否为空
After processing, determine if the head is empty
在循环删除 val 时,要使用一个pre变量表示 cur前的一个节点
When iteratively deleting val, use a pre variable to represent a node before cur

在处理 cur.val = val 时, prev.next = cur.next;
另外一种情况是, pre = cur; 更新 pre的位置

这两种情况都需要 移动 cur
cur = cur.next;

在这里插入图片描述

代码

class Solution {
    public ListNode removeElements(ListNode head, int val) {
        while (head != null && head.val == val) {
            head = head.next;
        }

        if(head == null) {
            return null;
        }

        ListNode pre = head;
        ListNode cur = head.next;

        while (cur != null) {
            if (cur.val == val) {
                pre.next = cur.next;
            } else {
                pre = cur;
            }

            cur = cur.next;
        }

        return head;
    }
}

思路简化

通过创建 一个dummyNode 在 head前面
避免对于 head.val == val的特殊情况的处理
By creating a dummyNode in front of the head
to Avoid handling special cases of head.val == val

class Solution {
    public ListNode removeElements(ListNode head, int val) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;

        ListNode cur = dummy;

        while (cur != null && cur.next != null) {
            if (cur.next.val == val) {
                cur.next = cur.next.next;
            } else {
                cur = cur.next;
            }
        }

        return dummy.next;
    }
}

Design链表 leetcode 707

链接

https://leetcode.com/problems/design-linked-list/

题目描述

Design your implementation of the linked list. You can choose to use a singly or doubly linked list.
A node in a singly linked list should have two attributes: val and next. val is the value of the current node, and next is a pointer/reference to the next node.
If you want to use the doubly linked list, you will need one more attribute prev to indicate the previous node in the linked list. Assume all nodes in the linked list are 0-indexed.

Implement the MyLinkedList class:

MyLinkedList() Initializes the MyLinkedList object.
int get(int index) Get the value of the indexth node in the linked list. If the index is invalid, return -1.
void addAtHead(int val) Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list.
void addAtTail(int val) Append a node of value val as the last element of the linked list.
void addAtIndex(int index, int val) Add a node of value val before the indexth node in the linked list. If index equals the length of the linked list, the node will be appended to the end of the linked list. If index is greater than the length, the node will not be inserted.
void deleteAtIndex(int index) Delete the indexth node in the linked list, if the index is valid.

代码

class MyLinkedList {
    Node head;
    Node tail;
    int cap;

    public MyLinkedList() {
        this.head = new Node(-1);
        this.tail = new Node(-1);
        this.cap = 0;

        this.head.next = tail;
        this.tail.prev = head;

    }

    public Node getNode(int index) {

        Node cur = head.next;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }

        return cur;
    }
    
    public int get(int index) {
        
        if(index < 0 || index >= cap) {
            return -1;
        }

        Node cur = getNode(index);

        return cur.val;

    }

    
    public void addAtHead(int val) {
        Node x = new Node(val);
        
        x.prev = this.head;
        x.next = head.next;

        head.next.prev = x;
        head.next = x;

        this.cap++;
    }
    
    public void addAtTail(int val) {
        Node x = new Node(val);

        x.prev = tail.prev;
        x.next = this.tail;

        tail.prev.next = x;
        tail.prev = x;

        this.cap++;
    }
    
    public void addAtIndex(int index, int val) {

        if (index > this.cap || index < 0) return;

        Node x = new Node(val);

        Node cur = getNode(index);

        Node p1 = cur.prev;
        Node p2 = cur;

        p1.next = x;
        p2.prev = x;

        x.prev = p1;
        x.next = p2;

        this.cap++;
    }
    
    public void deleteAtIndex(int index) {

        if (index >= this.cap || index < 0) return;

         Node cur = getNode(index);

        Node p1 = cur.prev;
        Node p2 = cur.next;

        p1.next = p2;
        p2.prev = p1;

        cur.next = cur.prev = null;

        this.cap--;
    }
}

class Node{
    int val;
    Node prev;
    Node next;

    public Node(int val) {
        this.val = val;
    }
}

/**
 * Your MyLinkedList object will be instantiated and called as such:
 * MyLinkedList obj = new MyLinkedList();
 * int param_1 = obj.get(index);
 * obj.addAtHead(val);
 * obj.addAtTail(val);
 * obj.addAtIndex(index,val);
 * obj.deleteAtIndex(index);
 */

思路

设计一个linkedlist
Design a linkedlist
实现增删插改
Realize addition, deletion, insertion and modification

create Node class first
suggest use doubleNode
own three attribution
val
prev
next

create DoubleLinkedList
head
tail
size

don’t forget to update size,
when each time you implentent the insert, add, remove function

犯错点

对于get,insert, remove等方法
开始要进行index的检查
For get, insert, remove and other methods
need index check.

get 和 remove 检查 index是否 0 - size - 1内
Get and remove check whether the index is 0 - size - 1
insert 检查 index是否 0 - size内
Insert to check if the index is within 0- size
使用cur = head.next
for (i =0 ;i<index;i++ ) cur = cur.next;
就可以找到该节点位置

可以单独创建getNode方法使用
因为要经常使用这个
You can create the getNode method separately to use
Because we need to use this frequently

通过 getNode找到cur点之后

先处理 x 和 prev 和 cur关系

链表反转 Reverse LinkedList

思路

  1. consider the spcial case
  2. use two pointer

cur and prev to reverse the linkedlist

代码

指针

class Solution {

    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) return head;

        ListNode cur = head, prev = null;

        while (cur != null) {
            ListNode next = cur.next;
            cur.next = prev;
            prev = cur;
            cur = next;
        }

        return prev;
    }
}

递归

这里定义的 reverseList
输入为 head
输出为 反转 head开头的链表

last = reverseList(head.next)
反转 head.next 开头的链表

head.next 是 head下一个指针
head.next.next = head 让head下一个指针指向 head
head.next = null 让head指向null节点

class Solution {

    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) return head;

        ListNode last = reverseList(head.next);
        head.next.next = head;
        head.next = null;

        return last;
    }
}

指针变得递归

class Solution {

    public ListNode reverseList(ListNode head) {
        return reverse(null, head);
    }

    ListNode reverse(ListNode prev, ListNode cur) {
        if (cur == null) return prev;

        ListNode next = cur.next;
        cur.next = prev;
        
        return reverse(cur, next);
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/463961.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【SpringBoot源码剥析】| 依赖管理

目录 一. &#x1f981; 依赖管理Ⅰ. 部分dependency导入时为啥不需要指定版本&#xff1f;1.1 父依赖启动器的工作1.2 问题答案 Ⅱ. 项目运行依赖的JAR包是从何而来的?2.1 分析源码2.2 问题答案 二. &#x1f981; 总结 一. &#x1f981; 依赖管理 Ⅰ. 部分dependency导入时…

Linux——中断和时间管理(中)

目录 驱动中的中断处理 中断下半部 软中断 tasklet 工作队列 驱动中的中断处理 通过上一节的分析不难发现&#xff0c;要在驱动中支持中断&#xff0c;则需要构造一个 struct irqaction的结构对象&#xff0c;并根据IRQ 号加入到对应的链表中(因为 irq_des 已经在内核初始…

golang微服务项目通用流水线

golang微服务项目通用流水线 工作中随着业务越来越大&#xff0c;微服务的项目也越来越多&#xff0c;最开始的时候是一个服务一个流水线&#xff0c;然后还分了三个环境&#xff0c;也就是一个服务三个流水线&#xff0c;后面就越来越不利于管理维护了&#xff0c;因此&#…

马云的创业故事及他人生中的摆渡人-创建阿里巴巴(五)

著名的“18罗汉大会” 以及“马云成功背后的男人” 1999年大年初五&#xff0c;杭州湖畔花园小区&#xff0c;18个人坐满了一屋子&#xff0c; 这是阿里巴巴的第一次全员大会&#xff0c;马云激情澎湃地讲了2个小时&#xff0c;并且专门请了摄影师全程录像。 这就是传说中的…

SD卡无法识别怎么办?

SD卡是一种可移动存储设备&#xff0c;广泛应用于各种电子设备&#xff0c;如Android智能手机、平板电脑或相机等&#xff0c;您可以将SD卡连接到计算机以传输一些文件。但有些时候&#xff0c;当您打开文件资源管理器后&#xff0c;可能会发现您的SD卡不显示&#xff0c;无法使…

即时通讯IM源码应该如何做好安全防护?

即时通讯&#xff08;Instant Messaging&#xff0c;IM&#xff09;在现代社会中已经成为了人们日常生活中必不可少的沟通工具。无论是在家庭、教育、商业或政府等各行各业中&#xff0c;IM都扮演着重要的角色。然而&#xff0c;随着IM使用率的增加&#xff0c;相应的安全威胁也…

程序员基础的硬件知识(cpu、主板、显卡、内存条等)

一、综合简介 cpu&#xff1a;负责运算数据&#xff0c;就等于你的大脑运算速度。 显卡&#xff1a;本来没有显卡&#xff0c;后来因为大家对图片要求越来越高&#xff0c;视频要求越来越高&#xff0c;啥都让cpu算太累了&#xff0c;于是分出来一个&#xff0c;专门用来计算…

华为云服务EulerOS release 2.0 版本安装大象数据库

1连接华为服务器 下载并按照命令yum install -y postgresql-server 2 初始化 postgresql-setup initdb 3启动 systemctl start postgresql.service 启动服务 4开放端口(如果防火墙已经关闭则可以省略) iptables -I INPUT -p tcp --dport 5432 -j ACCEPT 5验证安装结果&…

Cortex-A7中断详解(二)

CP15协处理器 CP15协处理器一般用于存储系统管理&#xff0c;但是在中断中也会使用到&#xff0c;CP15协处理器一共有16个32位寄存器。CP15协处理器的访问通过以下指令完成&#xff1a; MRC&#xff1a;将CP15协处理器中的寄存器数据读到ARM寄存器中。MCR&#xff1a;将ARM寄…

坚持刷题2个月,终于去了梦寐以求的大厂....

写在前面 最近一个读者和我反馈&#xff0c;他坚持刷题2个月&#xff0c;终于去了他梦寐以求的大厂&#xff0c;薪资涨幅非常可观&#xff0c;期间面字节跳动还遇到了原题…并表示目前国内的大厂和一些独角兽&#xff0c;已经越来越效仿硅谷公司的做法&#xff0c;通过面试给定…

软件测试的测试用例

1.白盒和黑盒测试: 黑盒测试&#xff1a;把代码看成一个黑盒子&#xff0c;只关心输入和输出结果之间的关系 产品功能是否符合要求&#xff1b; 白盒测试&#xff1a;能够看到代码本身&#xff0c;针对代码本身进行测试&#xff0c;测试代码本身的逻辑是否符合规范。 2.测试用…

常用图标(icon)css下载

1、演示图例&#xff08;icon1.css&#xff09;[24*18] 2、演示图例&#xff08;icon2.css&#xff09;[24*24] 3、演示图例&#xff08;icon3.css&#xff09;[24*24] 4、演示图例&#xff08;icon4.css&#xff09;[24*18] 5、演示图例&#xff08;icon5.css&#xff09;[26*…

C/C++每日一练(20230426)

目录 1. 不喜欢带钱的小C &#x1f31f;&#x1f31f; 2. 数组排序 ※ 3. 超级素数 ※ &#x1f31f; 每日一练刷题专栏 &#x1f31f; Golang每日一练 专栏 Python每日一练 专栏 C/C每日一练 专栏 Java每日一练 专栏 1. 不喜欢带钱的小C 小C不喜欢带钱&#xff0c…

linux 安装zsh shell工具

安装zsh sudo apt install zsh chsh -s /bin/zsh sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)" 这一步需要网络状态比较好 ~$ git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf ~$ ~/.fzf/i…

USB2.0、USB3.0和typec引脚定义

USB2.0 USB 2 (Type A) pinout PinNameDirectionColorDescription1Vred5 V power2D-←→whiteData -3D←→greenData 4GNDblackGround USB 2 (Type B) pinout PinNameDirectionColorDescription1Vred5 V power2D-←→whiteData -3D←→greenData 4GNDblackGround USB Mini/M…

输入 jupyter notebook 报错 ModuleNotFoundError: No module named ‘pysqlite2‘ 解决方案

今天在cmd命令行中输入jupyter notebook想要打开jupyter时&#xff0c;出现了以下问题&#xff1a;即找不到模块‘pysqlite2’。 找到出问题的文件“sessionmanager.py”&#xff0c;发现出问题的地方在于&#xff1a;尝试导入sqlite3没有导致失败 因此&#xff0c;以下是解决…

HCIA-RS实验-ENSP设备的基础配置

本文主要简单地介绍ENSP设备的基础配置&#xff0c;帮助读者快速上手使用ENSP。可以掌握一些基础的配置方案&#xff0c;更改名称&#xff0c;系统时间&#xff0c;系统地区、密码登录等信息 以下是该文章的拓扑图&#xff1b;现将这2台设备启动&#xff1b;后续双击即可进入命…

mac软件卸载不干净怎么回事 mac如何卸载软件干净

很多苹果用户会发现&#xff0c;mac卸载软件不干净。明明是早都卸载的软件还能看到那些软件的图标和残留文件夹。mac软件卸载不干净怎么回事&#xff1f;mac如何卸载软件干净&#xff1f;今天小编就来教大家如何将软件彻底卸载&#xff0c;保证电脑磁盘的干净。 一、mac软件卸…

“分布式基础概念”全面解析,让你秒懂分布式系统!【一】

前言 在项目中学习这些技术、加深了对其的使用和深层次的理解。以下总结来自谷粒商城项目案例资料 1、什么是微服务&#xff1f; 微服务架构风格&#xff0c;就像是把一个单独的应用程序开发为一套小服务&#xff0c;每个小服务运行在自己的进程中&#xff0c;并使用轻量级机…

Nacos单机搭建并集成项目

Nacos概述 Nacos Nacos是阿里巴巴开源的服务注册中心及配置中心&#xff0c;致力于给开发者提供一款便捷、简单上手的开源框架 Nacos注册中心Eureka 服务配置Config 服务总线Bus 服务发现和服务健康监测 Nacos 使服务更容易注册&#xff0c;并通过DNS或HTTP接口发现其他…