当前位置:网站首页>[sword finger offer] interview question 24 Reverse linked list
[sword finger offer] interview question 24 Reverse linked list
2022-06-13 04:22:00 【LuZhouShiLi】
Interview questions 24. Reverse a linked list
subject
Define a function , Enter the head node of a linked list , Invert the linked list and output the head node of the inverted linked list .
Ideas
Define three pointers ,pre,cur,temp; When I need to adjust nodes cur Of next When the pointer , Besides knowing the nodes i In itself , You also need to know its previous node h, The nodes i Of next The pointer points to the node h, We also need to save the nodes i The next node of J, To prevent the linked list from being disconnected . When we put nodes i Of next Point to previous node h When , At this time will be Pre Pointer to i,cur The pointer points to the node temp;
Code
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode *current = head,*pre = nullptr;
while(current != nullptr)
{
ListNode *tmp = current->next;// Store the next node
current->next = pre;// Set the... Of the current node next Point to previous node
// After the modification The current node becomes pre
pre = current; // pre Staging the current node
current = tmp; // Current pointer to temporary node
}
return pre;
}
};
边栏推荐
- 高等数学(第七版)同济大学 习题1-3 个人解答
- 10 minutes to thoroughly understand how to configure sub domain names to deploy multiple projects
- 1.4.2 Capital Market Theroy
- Intervention analysis + pseudo regression
- MCU: EEPROM multi byte read / write operation sequence
- MySQL索引
- Single chip microcomputer: infrared remote control communication principle
- 基于DE2-115平台的VGA显示
- Sword finger offer 11 Minimum number of rotation array - binary lookup
- [test development] automated test selenium (II) -- common APIs for webdriver
猜你喜欢
10 minutes to thoroughly understand how to configure sub domain names to deploy multiple projects
Tree array explanation
leetcode. 1 --- sum of two numbers
电磁兼容常用名词术语
Translation of ego planner papers
EMC rectification outline
The data obtained from mongodb query data command is null
February 25, 2021 (Archaeology 12 year Landbridge provincial competition)
[note]vs2015 compilation of masm32 using MASM32 Library
环评图件制作-数据处理+图件制作
随机推荐
Modeling discussion series 143 data processing, analysis and decision system development
Call C function in Lua
Common encryption and decryption function encapsulation - AES encryption and decryption
MVP framework for personal summary
LVS four layer load balancing cluster (3) cluster function classification - HPC
120. triangle minimum path sum - Dynamic Planning
Line height equals height why not center
5g China Unicom ap:b SMS ASCII transcoding requirements
R: Employee turnover forecast practice
Understand the pseudo static configuration to solve the 404 problem of refreshing the page of the deployment project
十亿数据量 判断元素是否存在
EGO planner论文翻译
高等数学(第七版)同济大学 习题1-2 个人解答
Unity Shader 学习 004-Shader 调试 平台差异性 第三方调试工具
Real time question answering of single chip microcomputer / embedded system
MySQL索引
[test development] installation of test management tool Zen path
PAT 1054 The Dominant Color
10 minutes to thoroughly understand how to configure sub domain names to deploy multiple projects
[Yugong series] June 2022 Net architecture class 080 master cluster and database switching of distributed middleware schedulemaster