当前位置:网站首页>链表--------------------尾插法
链表--------------------尾插法
2022-07-29 05:23:00 【李汁汁】
首先要定创建一个链表的实例,用来传入值
ListNode temp1 = new ListNode(value);
首先判断是否有头节点 如果没有的话 ,第一个值就是头节点
if(head==null) {head = temp1; return;}
如果head的next是空的
ListNode temp2 = head;
temp2.next =temp1;
如果headnext不是空
while(temp2.next!=null) {
temp2 = temp2.next;
}
所有代码图
ListNode:
public class ListNode {
int value;
ListNode next;//下一个对象的额地址域
public ListNode(int value){
this.value = value;
}
@Override
public String toString() {
return "ListNode [value=" + value + ", next=" + next + "]";
}
}Test:
public class Test {
public static void main(String[] args) {
LinkList linkList = new LinkList();
linkList.insert(1);
linkList.insert(2);
linkList.insert(3);
linkList.insert(4);
linkList.insert(5);
System.out.println(linkList.head);
}
}
public class LinkList {
public ListNode head ;
public void insert(int value) {
ListNode temp1 = new ListNode(value);
if(head==null) {
head = temp1;
return;
}
ListNode temp2 = head;
while(temp2.next!=null) {
temp2 = temp2.next;
}
temp2.next = temp1;
}
}
=
边栏推荐
- 华为云14天鸿蒙设备开发-Day3内核开发
- Jingwei Qili: development of heart rate and blood oxygen module based on hmep060 (1: FPGA sends multi bit instructions)
- 华为云14天鸿蒙设备开发-Day1源码获取
- HAL库学习笔记-14 ADC和DAC
- Error importing Spacy module - oserror: [e941] can't find model 'en'
- markdown与Typora
- 低功耗蓝牙5.0芯片nrf52832-QFAA
- 基于STC51:四轴飞控开源项目原理图与源码(入门级DIY)
- Model building in pytorch
- DP4301—SUB-1G高集成度无线收发芯片
猜你喜欢
随机推荐
HAL库学习笔记-10 HAL库外设驱动框架概述
2022 spring move - core technology FPGA development post pen test question (original question and experience)
给二维表添加时间序列索引
Hal library learning notes-13 application of I2C and SPI
【软件工程之美 - 专栏笔记】16 | 怎样才能写好项目文档?
【软件工程之美 - 专栏笔记】“一问一答”第3期 | 18个软件开发常见问题解决策略
Hal library learning notes-11 I2C
【软件工程之美 - 专栏笔记】24 | 技术债务:是继续修修补补凑合着用,还是推翻重来?
Reading papers on false news detection (I): fake news detection using semi supervised graph revolutionary network
基于F407ZGT6的WS2812B彩灯驱动
TB6600+stm32F407测试
QT学习笔记-Qt Model/View
QT learning notes - Import and export of Excel
Hal library learning notes-10 overview of Hal library peripheral driver framework
HR面必问问题——如何与HR斗志斗勇(收集于FPGA探索者)
QT learning notes QT model/view
【软件工程之美 - 专栏笔记】“一问一答”第2期 | 30个软件开发常见问题解决策略
Huawei cloud 14 day Hongmeng device development -day5 drive subsystem development
八大排序-----------------堆排序
【软件工程之美 - 专栏笔记】20 | 如何应对让人头疼的需求变更问题?









