设计链表复习

news/2024/10/21 15:32:53/
  1. 设计链表
    在这里插入图片描述
class ListNode {int val;ListNode next;public ListNode() {}public ListNode(int val) {this.val = val;}public ListNode(int val, ListNode next) {this.val = val;this.next = next;}}class MyLinkedList {//size存储链表元素的个数int size;//虚拟头节点ListNode head;//初始化链表public MyLinkedList() {size = 0;head = new ListNode(0);}//获取第index个节点的数值,注意index是从0开始的,第0个节点就是头结点public int get(int index) {if (index < 0 || index >= size) {return -1;}//当前指针先指向虚拟头节点ListNode cur = head;for (int i = 0; i <= index; i++) {cur = cur.next;}return cur.val;}//在链表最前面插入一个节点,等价于在第0个元素前添加public void addAtHead(int val) {addAtIndex(0, val);}public void addAtTail(int val) {addAtIndex(size, val);}public void addAtIndex(int index, int val) {if (index > size) {return;}if (index < 0) {index = 0;}size++;ListNode pre = head;for (int i = 0; i < index; i++) {//举个极端的在末尾插入的例子,如数组[1],在index为1处插入元素,循环结束会出现pre指向null的情况,导致引用失效,所以i不能=indexpre = pre.next;}ListNode toAdd = new ListNode(val);toAdd.next = pre.next;pre.next = toAdd;}public void deleteAtIndex(int index) {if (index < 0 || index >= size) {return;}size--;if (index == 0) {head = head.next;return;}ListNode pre = head;for (int i = 0; i < index; i++) {//极端例子,考虑到pre和pre.next都不能为空;如[1,2,3]删除index为2的元素,pre = pre.next;}pre.next = pre.next.next;}
}/*** 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);*/

http://www.ppmy.cn/news/1169249.html

相关文章

STM32串口

前言 提示&#xff1a;这里可以添加本文要记录的大概内容&#xff1a; 目前已经学习了GPIO的输入输出&#xff0c;但是没有完整的显示信息&#xff0c;最便宜的显示就是串口。 000 -111 AVR单片机 已经学会过了&#xff0c; 提示&#xff1a;以下是本篇文章正文内容&#x…

重生奇迹mu“荣誉之城”勇者大陆

曾经&#xff0c;不少重生奇迹mu玩家讨论最经典的新人出生地&#xff0c;有的说是仙踪林&#xff0c;有的则是说勇者大陆&#xff0c;最后在重生奇迹mu网站上面&#xff0c;以投票的方式最终得出一个答案&#xff0c;那就是勇者大陆&#xff0c;游戏里面当之无愧的荣誉之城&…

Spring中事务失效的几种场景及解决办法

未抛出异常&#xff1a;如果在一个带有事务的方法中没有抛出异常&#xff0c;Spring无法检测到事务失败&#xff0c;从而无法回滚。解决方法是确保在事务中遇到错误时抛出异常。 异常被捕获&#xff1a;如果在一个带有事务的方法中抛出异常&#xff0c;但被捕获并处理了&#…

CANOE CAPL编程采坑记录---capl中的函数中的变量,默认是静态局部变量

在一个capl文件中&#xff0c;我需要写一个checksum校验的函数&#xff0c;用来在发送报文时给最后一个字节checksum字段赋值&#xff0c;函数大体如下&#xff1a; byte Send_CheckSum(word msgId, byte data[], byte len) //data为数组&#xff0c;len为数组长度 { byte i, …

Leetcode—34.在排序数组中查找元素的第一个和最后一个位置【中等】

2023每日刷题&#xff08;六&#xff09; Leetcode—34.在排序数组中查找元素的第一个和最后一个位置 实现代码 /*** Note: The returned array must be malloced, assume caller calls free().*/ int lower_bound(int *arr, int numsSize, int target) {// 左闭右开区间[lef…

6.MySQL内置函数

个人主页&#xff1a;Lei宝啊 愿所有美好如期而遇 日期函数 current_date() 当前日期 select 可以做表达式和函数的计算。 current_time() 当前时间 current_timestamp() 当前日期加时间 注意&#xff1a;值得说明的是这三个函数底层调用的都是同一个函数&#xff0c;只不…

wps excel js编程

定义全局变量 const a "dota" function test() {Debug.Print(a) }获取表格中单元格内容 function test() {Debug.Print("第一行第二列",Cells(1,2).Text)Debug.Print("A1:",Range("A1").Text) }写单元格 Range("C1").Val…

effective c++学习笔记(后四章)

六 继承与面向对象设计 红色字 \color{FF0000}{红色字} 红色字 32 确定你的public继承塑模出 is-a关系 如果你令class D (“Derived”)以public形式继承class B (“Base”)&#xff0c;你便是告诉C编译器&#xff08;以及你的代码读者&#xff09;说&#xff0c;每一个类型为…