当前位置:网站首页>单链表面试题
单链表面试题
2022-07-29 05:24:00 【Tting_12】
class HeroNode{
public int no;
public String name;
public String nickname;
public HeroNode next;
//构造器
public HeroNode(int no, String name, Stirng nickname){
this.no = no;
this.name = name;
this.nickname = nickname;
}
}
新浪面试题---查找单链表中的倒数第k个节点
思路:
1.编写一个方法,接受head节点,同时接收一个index
2.index表示倒数第index节点
3.先把链表从头到尾遍历,得到链表的节点个数
4.得到size后,从链表的第一个节点开始遍历,遍历 size-index 个节点
//getLength()方法是自己编写的获取链表节点个数方法
public static HeroNode findLastIndexNode(heroNOde head, int index){
if(head.next == null){
return null;
}
int size = getLength(head);
if(index <= 0 || index > size){
return null;
}
heroNode cur = head.next;
for(int i = 0; i < size - index; i++){
cur = cur.next;
}
return cur;
}
腾讯面试题---将单链表反转
思路
1.先定义一个节点reverseHead = new HeroNode();
2.从头到尾遍历原来的链表,每遍历一个节点就将其取出,并放在新的链表reverseHead的最前端
3.原来的链表的head.next = reverseHead.next
public static void reversetList(HeroNode head){
//如果当前链表为空或者只有一个节点,无需反转直接返回
if(head.next == null || head.next.next ==null){
return;
}
//
HeroNode cur = head.next;
HeroNode next = null; //当前节点的下一个节点
HeroNode reversHead = new HeroNode(0,"","");
While(cur != null){
next = cur.next;
cur.next = reverseHead.next;
reverseHead.next = cur;
cur = next;
}
head.next = reverseHead.next;
}
百度面试题---从尾到头打印链表
思路:
上面的要求是逆序打印链表
如果先反转再打印会破坏原来的链表不建议
我么可以利用栈这个数据结构,将各个节点压入到栈中,利用栈的先进后出的特点实现逆序打印
边栏推荐
- HAL库学习笔记- 8 串口通信之概念
- CS4344国产替代DP4344 192K 双通道 24 位 DA 转换器
- 基于51单片机的直流电机调速系统(L298的使用)
- 爬取表情包
- 2.4G频段的无线收发芯片 SI24R1 问题汇总解答
- Based on stc51: schematic diagram and source code of four axis flight control open source project (entry-level DIY)
- TLE5012b+STM32F103C8T6(bluepill)读取角度数据
- ML7 self study notes
- Rowkey设计
- 125KHz唤醒功能2.4GHz单发射芯片-Si24R2H
猜你喜欢
Ml4 self study notes
STM32FF030 替代国产单片机——DP32G030
HAL库学习笔记-10 HAL库外设驱动框架概述
八大排序-----------快速排序
Open source based on STM32: MHD Bluetooth speaker (including source code +pcb)
封装——super关键字
倾角传感器用于通信铁塔、高压电塔长期监测
Based on STM32: couple interactive doll (design scheme + source code +3d drawing +ad circuit)
Ml8 self study notes LDA principle formula derivation
基于51单片机的DAC0832波形发生器
随机推荐
基于DAC0832的直流电机控制系统
Huawei cloud 14 days Hongmeng device development -day1 environment construction
HAL学习笔记 - 7 定时器之高级定时器
scanBasePackages扫包范围配置
低功耗蓝牙5.0芯片nrf52832-QFAA
NoClassDefFoundError 处理
Hal library learning notes-13 application of I2C and SPI
125KHz唤醒功能2.4GHz单发射芯片-Si24R2H
DP1332E多协议高度集成非接触式读写芯片
2022 spring recruit - Hesai technology FPGA technology post (one or two sides, collected from: Digital IC workers and FPGA Explorers)
数学建模心得
兼容cc1101/cmt2300-DP4301 SUB-1G 无线收发芯片
Model building in pytorch
新能源共享充电桩管理运营平台
智慧能源管理系统解决方案
STM32 检测信号频率
STM32FF030 替代国产单片机——DP32G030
基于msp430f2491的proteus仿真(实现流水灯)
Ml9 self study notes
JUC并发知识点