Java链表常见操作【剑指Offer】03:从尾到头打印链表
2024-09-02 23:29:49
题目描述
输入一个链表,按链表从尾到头的顺序返回一个ArrayList。
题解一:递归
/*
在最后一次递归方法返回以后,每一层的递归方法都会做一个arrayList.add(listNode.val)这个操作,
从最后一次到第一次,逆向的调用了后面的方法
*/
static ArrayList<Integer> list = new ArrayList<>();
public static ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
if (listNode != null) {
printListFromTailToHead(listNode.next);
list.add(listNode.val);
}
return list;
}
题解二:reverse()方法
public static ArrayList<Integer> printListFromTailToHead01(ListNode listNode) {
ArrayList<Integer> list = new ArrayList<Integer>();
while(listNode != null){
list.add(listNode.val);
listNode = listNode.next;
}
Collections.reverse(list);//使用Collections的reverse方法,直接将list反转
return list;
}
节点结构定义
public static class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
从头插入节点
public static void insetFromHead(ListNode head,ListNode newNode){
newNode.next=head;
head = newNode;
}
在尾部插入节点
public static void insertFromTail(ListNode head, ListNode newNode){
//如果是个空链表,直接把新节点赋值给head,然后结束,要先判断null的情况
if(head == null){
head =newNode;
return;
}
//用temp代替head去遍历找到最后一个节点,一定不要用head自己去遍历,
ListNode temp = head;
while (temp.next!=null){
temp=temp.next;
}
temp.next=newNode;
}
计算链表的长度
public static int length(ListNode head){
int len =0;
ListNode temp = head;
while(temp!=null){
len++;
temp=temp.next;
}
return len;
}
按照顺序输出一个列表
public static void printList(ListNode head){
ListNode temp = head;
while(temp != null){
System.out.print(temp.val+" ");
temp = temp.next;
}
System.out.println();
}
从特定位置删除一个节点
public static boolean deleteFromIndex(ListNode head,int index){
if(index<1||index>length(head)){ //先判断是否越界
return false;
}
if(index ==1){//如果是删除第一个元素,因为直接涉及到了head所以只能单独处理
head = head.next;
return true;
}
ListNode curNode = head;
//删除顺序为index的node只能将curNode停在index-1的位置
for(int curIndex =1;curIndex<index-1;curIndex++){
curNode = curNode.next;
}
curNode.next=curNode.next.next;
return true;
}
测试:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int nums = sc.nextInt();
ListNode headNode = new ListNode(1);
for(int i=2;i<=nums;i++){
insertFromTail(headNode,new ListNode(i));
}
printList(headNode);
ArrayList<Integer> list = printListFromTailToHead01(headNode);
System.out.println(list);
ArrayList<Integer> list1 = printListFromTailToHead(headNode);
System.out.println(list1);
deleteFromIndex(headNode,3);
printList(headNode);
int length = length(headNode);
System.out.println(length);
}
输入:8
输出:
1 2 3 4 5 6 7 8
[8, 7, 6, 5, 4, 3, 2, 1]
[8, 7, 6, 5, 4, 3, 2, 1]
1 2 4 5 6 7 8
7
最新文章
- HashMap的实现原理
- if条件语句
- part 设置
- 由浅入深学习.NET CLR 系列:目录
- 安全验证之使用摘要认证(digest authentication)
- LINUX下编译安装最新版本mysql
- Abp(.NetCore)开发与发布过程
- JAVA IO分析三:IO总结&;文件分割与合并实例
- Apollo单向SSL认证(1)
- 版本名称GA的含义:SNAPSHOT->;alpha->;beta->;release->;GA
- Lodop打印控件不打印css背景图怎么办
- datatime
- iOS 11开发教程(十九)iOS11应用视图美化按钮之设置按钮的外观
- c++ linux socket编程 c++网络编程
- Vue 数据响应式原理
- mac 上位Idea 配置Project SDK
- OpenGL ES 3.0顶点着色器(一)
- MongoDB 学习笔记(1)
- Web程序中的懒加载异常说明及解决方案
- kbmMW CopyRawRecords 用法
热门文章
- Spring——管理Bean的生命周期
- Python Special Methods - 特殊方法
- 大神是如何学习 Go 语言之 Channel 实现原理精要
- [redis读书笔记] 第一部分 数据结构与对象 对象特性
- 14-SSM整合
- 为什么Linux 实例执行 df 和 du 查看磁盘时结果不一致
- 编译安装nginx提示./configure: error: C compiler cc is not found
- [Redis-CentOS7]Redis安装(-)
- MATLAB添加工具箱及无法连接到MathWorks问题
- 用C语言实现中国象棋