当前位置:网站首页>Sword finger offer 06 Print linked list from end to end
Sword finger offer 06 Print linked list from end to end
2022-06-10 15:38:00 【soO_ 007】
subject :
Enter the head node of a linked list , Return the value of each node from the end to the end ( Return with array ).
Example 1:
Input :head = [1,3,2]
Output :[2,3,1]
Limit :
0 <= Chain length <= 10000
source : Power button (LeetCode)
link :https://leetcode.cn/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Ideas :
I've been relatively idle recently , Think again, sword finger . This is a simple question , The common idea is to go through it list Deposit in vector, Again reverse vector; Or on the spot reverse list, Re deposit vector; Or deposit stack Again pop come out , These differences are not big . Wrote the simplest version of the code 1. And then when I look at other people's answers , I saw a version of powerful recursion , Put it in the code 2 了 . Indeed, this problem is very clear in the case of a small amount of data , But I don't usually think of . here base case Namely head It's empty , Go straight back to an empty vector, And every time we get vector Are returned by the next level of recursion , Because it's in reverse order , We will return to the current layer after the next layer returns val In the vector The end of , This returns vector It's in reverse order .
Code 1:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
vector<int> reversePrint(ListNode* head) {
vector<int> record;
while (head) {
record.push_back(head->val);
head = head->next;
}
reverse(record.begin(), record.end());
return record;
}
};
Code 2:
边栏推荐
- The power of insight
- How the terminator sets the font to display different colors
- docket命令
- 剑指 Offer 06. 从尾到头打印链表
- C# 游戏雏形 人物地图双重移动
- Comment construire un plan de produit axé sur le client: conseils du CTO
- Beginner pytorch step pit
- Hutool Usage Summary (VIP collection version)
- opencv#4 手写体识别:自建训练集完美
- Opentelemetry metrics release candidate
猜你喜欢

100003 words, take you to decrypt the system architecture under the double 11 and 618 e-commerce promotion scenarios

一文带你了解J.U.C的FutureTask、Fork/Join框架和BlockingQueue

ORB_SLAM2视觉惯性紧耦合定位技术路线与代码详解1——IMU流型预积分

uniapp中常用到的方法(部分) - 時間戳問題及富文本解析圖片問題

VINS理论与代码详解4——初始化

How to build a customer-centric product blueprint: suggestions from the chief technology officer

从“初代播种”到“落地生花”,广和通在5G商用三年间做了什么?

数字化管理中台+低代码,JNPF开启企业数字化转型的新引擎

Fast detection of short text repetition rate

The product design software figma cannot be used. What software with similar functions is available in China
随机推荐
Kubernetes 1.24: avoid conflicts when assigning IP addresses to services
Docket command
docket命令
统一认证中心 Oauth2 认证坑
Cap version 6.1 Release Notice
Li Kou daily question - day 18 -350 Intersection of two data Ⅱ
VINS理論與代碼詳解4——初始化
How does the wireless communication module help the intelligent UAV build the "Internet of things in the air"?
Explore the secrets behind the open source data visualization development platform flyfish!
Hessian matrix of convex function and Gauss Newton descent method
Mitm (man in the middle attack)
ORB_ Slam2 visual inertial tight coupling positioning technology route and code explanation 2 - IMU initialization
Hutool Usage Summary (VIP collection version)
Interview question details
Technology sharing | quick intercom, global intercom
信息论与编码2 期末复习-BCH码
Explain the opencv function filter2d() in detail and remind you that the operation it does is not convolution but correlation operation
This article introduces you to j.u.c's futuretask, fork/join framework and BlockingQueue
Remote monitoring and data acquisition solution
Day10/11 递归 / 回溯