当前位置:网站首页>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:
边栏推荐
- opencv#4 手写体识别:自建训练集完美
- Detailed explanation of binary search
- ORB_ Slam2 visual inertial tight coupling positioning technology route and code explanation 3 - tight coupling optimization model
- Guanghetong cooperates with China Mobile, HP, MediaTek and Intel to build 5g fully connected PC pan terminal products
- 数据库创建触发器的问题
- 2022 Nanjing International Smart site equipment exhibition
- "Bloom Cup" 5g Application Award grand slam! Several joint projects of guanghetong won the first, second and third prizes in the general product theme competition
- 100003 words, take you to decrypt the system architecture under the double 11 and 618 e-commerce promotion scenarios
- A complete multi-user wechat public platform development source code, with free sharing of documents
- 探索数据可视化开发平台FlyFish开源背后的秘密!
猜你喜欢

Vins theory and code explanation 4 - initialization

How to improve document management

The ultimate buff of smart grid - guanghetong module runs through the whole process of "generation, transmission, transformation, distribution and utilization"

港大、英伟达 | Factuality Enhanced Language Models for Open-Ended Text Generation(用于开放式文本生成的事实性增强语言模型)

3、再遇HandyControl之窗体

What has guanghetong done in the three years of 5g business from "seeding in the first generation" to "flower on the ground"?

SQL语言

VINS理論與代碼詳解4——初始化

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

Explore the secrets behind the open source data visualization development platform flyfish!
随机推荐
数字化管理中台+低代码,JNPF开启企业数字化转型的新引擎
cmake实战记录(一)
Golang []byte to file
Create a space of local value together. In 2022, China successfully held the "one hundred cities tour · Ningbo Station" for commercial distribution
Wechat applet slides to the top
反“内卷”,消息称 360 企业安全云将上线“一键强制下班”功能,电脑自动关闭办公软件
Applet network request promise
[rust daily] first release of mnemos on April 20, 2022
农产品期货如何开户?有没有什么资金条件?
自动化运维必备的工具-Shell脚本介绍
The ultimate buff of smart grid - guanghetong module runs through the whole process of "generation, transmission, transformation, distribution and utilization"
Technology sharing | quick intercom, global intercom
VINS理論與代碼詳解4——初始化
如何构建以客户为中心的产品蓝图:来自首席技术官的建议
Comment construire un plan de produit axé sur le client: conseils du CTO
One-way hash function
The power of insight
HKU and NVIDIA | factuality enhanced language models for open ended text generation
We media video Hot Ideas sharing
Summary of 5 years' experience in ERP odoo privilege management system setup