编程导航算法通关村第 1关 | 单链表的操作

编程导航算法通关村第 1关 | 链表的操作

文章目录

  • 编程导航算法通关村第 1关 | 链表的操作
    • 单链表
      • 链表的定义
      • 初始化
      • 链表的遍历
      • 获取链表的长度
      • 链表的插入
      • 链表的节点的删除
    • 双向链表
      • 节点的定义
      • 双向链表的定义
      • 节点的打印
      • 获取长度
      • 头部插入元素
      • 尾部插入元素
      • 链表的删除

单链表

链表的定义

/*** 单链表节点的定义*/
public class ListNode {public int val;public ListNode next;ListNode(int x) {val = x;//这个一般作用不大,写了会更加规范next = null;}
}

初始化

  public static ListNode init(ListNode head) {int i = 2;ListNode temp = head;while (i <= 10) {ListNode listNode = new ListNode(i);temp.next = listNode;temp = temp.next;i++;}return head;}

链表的遍历

/*** 遍历链表*/public static void print(ListNode head) {ListNode temp = head;while (temp != null) {System.out.println(temp.val);temp = temp.next; // 向后移动指针}}

获取链表的长度

 /*** 获取链表的长度*/public static int getLength(ListNode head) {ListNode temp = head;int length = 0;while (temp != null) {temp = temp.next; // 向后移动指针length++;}return length;}

链表的插入

  • 在链表的插入中,我们需要考虑三种情况
  • 头结点前插入:直接将newNode的next指向head,然后将head指向为第一个节点(newNode)
 newNode.next = head;head = newNode;return head;
  • 尾结点后插入:将指针指向节点的末尾
 ListNode temp = head;
//            先将指针移动到最后while (temp.next != null) {temp = temp.next;}temp.next = newNode;return head;
  • 中间插入:需要将指针指向待插入位置的前一个节点
    在这里插入图片描述
//        中间插入ListNode temp = head;int i = 1;
//        将temp移动到前一个的位置,此处的位置应该是posttion-1while (i < posttion - 1) {temp = temp.next;i++;}
//        将新节点插入到temp之后newNode.next = temp.next;temp.next = newNode;return head;
  • 完整代码
 /*** 插入链表 postition的范围是1——length*/public static ListNode insert(ListNode head, ListNode newNode, int posttion) {
//        分为三种情况进行讨论
//        判断参数是否合法if (head == null) {return null;}if (newNode == null) {return null;}int length = getLength(head);if (posttion <= 0 || posttion > length) {return null;}//        头部插入if (posttion == 1) {newNode.next = head;head = newNode;return head;}
//        尾部插入if (posttion == length) {ListNode temp = head;
//            先将指针移动到最后while (temp.next != null) {temp = temp.next;}temp.next = newNode;return head;}
//        中间插入ListNode temp = head;int i = 1;
//        将temp移动到前一个的位置,此处的位置应该是posttion-1while (i < posttion - 1) {temp = temp.next;i++;}
//        将新节点插入到temp之后newNode.next = temp.next;temp.next = newNode;return head;}    

链表的节点的删除

  • 需要考虑三种情况
    • 节点是头节点
    • 节点是尾节点
    • 节点是中间节点
 /*** 单链表的删除*/public static ListNode delete(ListNode head, int posttion) {
//if (head == null) {return null;}if (posttion <= 0 || posttion > getLength(head)) {return null;}//        删除头结点if (posttion == 1) {head = head.next;return head;}
//    尾结点if (posttion == getLength(head)) {ListNode temp = head;while (temp.next.next != null) {temp = temp.next;}temp.next = null;
//return head;}//   删除中间节点ListNode temp = head;int i = 1;
//   移动到前一个while (i < posttion - 1) {temp = temp.next;i++;}//        删除他的下一个节点temp.next = temp.next.next;return head;}

双向链表

节点的定义

package com.lmx.project.first.bronze;class DoubleNode {public int data;    //数据域public DoubleNode next;    //指向下一个结点public DoubleNode prev;public DoubleNode(int data) {this.data = data;}//打印结点的数据域public void displayNode() {System.out.print("{" + data + "} ");}
}

双向链表的定义

  • 需要定义头结点、尾结点
  private DoubleNode first; // 头结点private DoubleNode last; // 尾结点public DoublyLinkList() {first = null;last = first;}

节点的打印

  * 正序打印*/public void printOrder() {DoubleNode temp = first;while (temp != null) {temp.displayNode();temp = temp.next;}}/*** 导入打印*/public void printRevereOrder() {DoubleNode temp = last;while (temp != null) {temp.displayNode();temp = temp.prev;}}

获取长度

  /*** 获取链表长度*/public int getLength() {int length = 0;DoubleNode temp = first;while (temp != null) {length++;temp = temp.next;}return length;}

头部插入元素

 /*** 头部插入元素*/public void insertFirst(int data) {DoubleNode newNode = new DoubleNode(data);
//        没有头结点的情况if (first == null) {first = newNode;last = first;return;}newNode.next = first;first.prev = newNode;first = newNode;}

尾部插入元素

  /*** 尾插入元素*/public void insertLast(int data) {DoubleNode newNode = new DoubleNode(data);if (first == null) {first = newNode;last = first;return;}newNode.prev = last;last.next = newNode;last = newNode;}
  • 中间插入元素
    在这里插入图片描述
  /*** 中间插入元素*/public void insert(int data, int position) {int length = getLength();if (position <= 0 || position > length) {return;}if (position == 1) {insertFirst(data);return;}if (position == length + 1) {insertLast(data);return;}DoubleNode newNode = new DoubleNode(data);
//       中间的插入DoubleNode temp = first;int i = 1;while (i < position - 1) {temp = temp.next;i++;}
//        将结点插入到temp后面temp.next.prev = newNode;newNode.next = temp.next;temp.next = newNode;newNode.prev = temp;}

链表的删除

在这里插入图片描述

/*** 链表的删除*/public DoubleNode delete(int key) {
//DoubleNode temp = first;while (temp != null && temp.data != key) {temp = temp.next;}if (temp == null) {return temp;}
//        如果是头结点if (temp == first) {first = first.next;first.prev = null;} else if (temp == last) {//        如果是尾结点last.prev.next = null;} else {//        如果是中间节点temp.next.prev = temp.prev;temp.prev.next = temp.next;temp.next = null;temp.prev = null;}return temp;}

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

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

相关文章

jenkins发布使用邮件添加审批

首先安装好Email Extension Plugin插件并在 system下配置好邮件 然后配置流水线需要的参数 ![在这里插入图片描述](https://img-blog.csdnimg.cn/418fc89bfa89429783a1eb37d3e4ee26.png#pic_center pipeline如下&#xff1a; def skipRemainingStages false //是否跳过生…

采集发布到WordPress网址(OneNav主题-WordPress主题)

WordPress系统的一导航主题&#xff08;OneNav主题&#xff09;是集网址、资源、资讯于一体的导航主题。 要将采集的数据批量自动发布到一导航主题&#xff08;OneNav主题&#xff09;的网址要怎么设置&#xff1f; 普通的文章采集器一般只能发布为wordpress文章类型&#xff…

自监督语义分割面模型——Masked Autoencoders Are Scalable Vision Learners(MAE)论文阅读

1、摘要 This paper shows that masked autoencoders (MAE) are scalable self-supervised learners for computer vision. Our MAE approach is simple: we mask random patches of the input image and reconstruct the missing pixels. It is based on two core designs. F…

Go语言之函数,返回值,作用域,传参,匿名函数,高阶函数,闭包函数

函数声明和调用 go语言是通过func关键字声明一个函数的&#xff0c;声明语法格式如下 func 函数名(形式参数) (返回值) {函数体return 返回值 // 函数终止语句 }函数名&#xff1a;由字母、数字、下划线组成。但函数名的第一个字母不能是数字。在同一个包内&#xff0c;函数名…

SpringBoot使用JWT进行身份验证

JWT身份验证的流程 用户登录&#xff1a; 用户向服务器提供他们的用户名和密码。 服务器验证&#xff1a;服务器接收到请求&#xff0c;验证用户名和密码。 生成JWT&#xff1a;如果用户名和密码验证通过&#xff0c;服务器将创建一个 JWT。 JWT 包含了一些数据&#xff08;称…

【activiti】工作流入门基础概念

Activiti 为什么使用activitiactiviti流程步骤表结构分四种&#xff1a;四个重要的服务类BusinessKey挂起流程变量&#xff1a;注意&#xff1a;网关&#xff1a;精细控制流程走向组任务分配&#xff1a;抄送&#xff1a;activiti工作流获取流程定义中所有的节点: 为什么使用ac…

[JVM] 5. 运行时数据区(2)-- 程序计数器(Program Counter Register)

一、概述 JVM中的程序计数器&#xff08;Program Counter Register&#xff09;是对物理PC寄存器的一种抽象模拟。它是一块很小的内存空间&#xff0c;几乎可以忽略不记。也是运行速度最快的存储区域。在 JVM 规范中&#xff0c;每个线程都有它自己的程序计数器&#xff0c;是…

redis之主从复制、哨兵、集群

文章目录 一、redis的高可用1.1 redis高可用的概念1.2 Redis的高可用技术 二、redis 主从复制2.1主从复制的原理2.2搭建Redis 主从复制 三、Redis 哨兵模式3.1搭建Redis 哨兵模式3.2启动哨兵模式3.3查看哨兵信息3.4故障模拟 四、Redis 群集模式4.1搭建Redis 群集模式 一、redis…

【Excel】excel多个单元格的内容合并到一个单元格,并使用分隔符

方法一&#xff1a;使用连接符 & 左键单击选中“D2”单元格&#xff0c;在D2单元格中输入公式“A2&B2&C2”&#xff0c;按“Enter”即可实现数据合并。 ------如果想连接的时候&#xff0c;中间加分隔符&#xff0c;可以使用&#xff1a;公式A2&"&#xf…

常用的rsync使用方式

发送文件夹并保持文件属性不变 rsync -avr path xxx192.168.x.x:~/path 关于rsync的说明&#xff1a; rsync is a file transfer program capable of efficient remote update via a fast differencing algorithm. Usage: rsync [OPTION]... SRC [SRC]... DEST or rsyn…

STL unordered_set的eraseyong fa

如下unordered_set的erase操作导致程序崩溃&#xff0c;crash。 #include <iostream> #include <string> #include <unordered_set> int main () { std::unordered_set<std::string> myset {"USA","Canada","France&qu…

Azure Kinect 之 Note(一)

Azure Kinect Azure Kinect DK 是一款开发人员工具包&#xff0c;配有先进的AI 传感器&#xff0c;提供复杂的计算机视觉和语音模型。 Kinect 将深度传感器、空间麦克风阵列与视频摄像头和方向传感器整合成一体式的小型设备&#xff0c;提供多种模式、选项和软件开发工具包(S…

面试题更新之-HTML5的新特性

文章目录 导文新特性有哪些&#xff1f;HTML5的新特性带来了许多好处 导文 面试题更新之-HTML5的新特性 新特性有哪些&#xff1f; HTML5引入了许多新特性和改进&#xff0c;以下是一些HTML5的新特性&#xff1a; 语义化标签&#xff1a;HTML5引入了一系列的语义化标签&#…

远程在Ubuntu20.04安装nvidia显卡驱动

第零步&#xff0c;找人装一个todesk。 在终端运行&#xff1a; ifconfig 记住ip地址&#xff0c;后面要用。 第一步&#xff0c;安装软件&#xff1a; sudo apt-get update sudo apt-get install g gcc make 第二步&#xff0c;下载显卡驱动&#xff1a; 官方驱动 | NVI…

【ThinkPHP】实现一个逆向工程生成model

ThinkPHP为了节省一些重复的步骤&#xff0c;写了个简单版的生成model的工具&#xff0c;逆向生成model代码&#xff0c;节省时间&#xff0c;专注写业务代码。 ThinkPHP中的命令行也提供了一些生成代码的命令&#xff1a; make:controller 创建控制器 make:model 创建模型 m…

医院制剂研发与真实世界评价论坛圆满闭幕

医院制剂是新药的摇篮和宝库&#xff0c;现代科技为医院制剂的研发和转化赋能。在新时代新政策下&#xff0c;2023年07月16日&#xff0c;由湖南省药学会医院制剂研发与真实世界评价专业委员会&#xff08;下称“专委会”&#xff09;主委单位湖南易能生物医药有限公司&#xf…

划片机的技术分解

划片机是一种切割设备&#xff0c;主要用于将硬脆材料&#xff08;如硅晶圆、蓝宝石基片、LED基片等&#xff09;分割成较小的单元。其工作原理是以强力磨削为划切机理&#xff0c;通过空气静压电主轴带动刀片与工件接触点的划切线方向呈直线运动&#xff0c;将每一个具有独立电…

MVVM模式的具体实现

MVVM即Model-View-ViewModel的简写。即模型-视图-视图模型。 模型&#xff08;Model&#xff09;指的是后端传递的数据。 视图(View)指的是所看到的页面。 视图模型(ViewModel)是mvvm模式的核心&#xff0c;它是连接view和model的桥梁。 它有两个方向&#xff1a; 一是将视图(V…

概率论的学习和整理18:为什么 P(至少成功1次) = Σ P(几何分布) ,总结几何分布和连续失败概率的关系,二项分布和累计成功k次的关系

目录 1 先说结论&#xff1a; 2 Σ几何分布的P(xn) P(n次试验至少成功1次) 2.1 几何分布的概率 2.2 这个是可以证明的&#xff0c;下面是推导过程 2.3 怎么理解呢&#xff1f; 3 另外&#xff0c;P(累计成功k次) ΣP(成功k次的二项分布) 3.1 成功k次的概率 和 累计成…

回收站怎么看当天删除的文件?在回收站中找不到被删除文件怎么回事

在日常使用电脑的过程中&#xff0c;我们常常会遭遇删除文件的错误&#xff0c;这时回收站就像是一剂“后悔药”。然而&#xff0c;当回收站中堆积了许多已删除的文件时&#xff0c;我们如何才能找到当天删除的文件呢&#xff1f;如果回收站在这时无法提供文件&#xff0c;我们…