当前位置:网站首页>Sword finger offer 06. print linked list from end to end
Sword finger offer 06. print linked list from end to end
2022-07-28 10:54:00 【jjj34】
Title Description

Ideas : Use the last in first out feature of stack to realize
1. Create a stack
2. Traversing the linked list , Stack the values corresponding to the linked list nodes
3. Declare an array , The length is the length of the stack , Out of the stack
Knowledge point : Stack , Linked list
Stack related knowledge points : Refer to this blog
Related knowledge points of linked list :

Pictured , The data structure of linked list contains three values ,
The first is an integer , That is, the value corresponding to the node
The second is the pointer , That is to point to the next node ( effect : Connect linked lists )

Pictured , Because of the pointer to the next node , Linked lists can be connected
The end node does not point to an object , The value of the pointer is null
The third is assignment , Assign values to the nodes in the linked list
The code is as follows
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public int[] reversePrint(ListNode head) {
// Create a stack , Take advantage of the last in first out feature of the stack
// Create a pointer to the head of the linked list
// Create an array , The length is the size of the stack
Stack<Integer> st;
st=new Stack<>();
while(head != null) // The pointer value of the tail node is null To determine whether the linked list is traversed
{
st.push(head.val);
head=head.next;
}
int n =st.size();
int result[] = new int [n];
int i=0;
while(!st.empty())
{
result[i]=st.pop();
i++;
}
return result;
}
}
边栏推荐
- JDBC各个类的解释
- PyQt5快速开发与实战 4.13 菜单栏、工具栏与状态栏 and 4.14 QPrinter
- 19. Delete the penultimate node of the linked list
- Lucene query syntax memo
- Judge whether the nixie tube is a common anode or a common cathode
- GKSphereObstacle
- PyQt5快速开发与实战 4.11 拖曳与剪贴板
- Markdown to word or PDF
- 5. Implement MapReduce program on window side to complete wordcount function
- GKBillowNoiseSource
猜你喜欢

11_ue4进阶_男性角色换成女性角色,并修改动画

GKRidgedNoiseSource

GKConstantNoiseSource

The 10th Landbridge cup embedded electronic provincial competition

Solving the optimal solution of particle swarm optimization

Product side data analysis thinking

Yan reports an error: exception message: /bin/bash: line 0: fg: no job control

PyQt5快速开发与实战 4.12 日历与时间

分体式测斜探头安装要点及注意事项

剑指 Offer 30. 包含min函数的栈
随机推荐
Status Notice ¶
适合中小企业的进销存软件,搞定5大难题
OCR 知识 概括
Attention 注意力机制流程框图
GKVoronoiNoiseSource
数组相关的知识点
GKBillowNoiseSource
这里有一份超实用Excel快捷键合集(常用+八大类汇总)
Solving the optimal solution of particle swarm optimization
GKSpheresNoiseSource
Samba server configuration
图片滑动特效
OCR knowledge summary
Lucene query syntax memo
GKNoise
零代码 | 轻松实现数据仓库建模,搭建BI看板
Install GMP
Blue Bridge Cup embedded Hal library systick
哈希表的相关知识点
JSON初步理解