当前位置:网站首页>Linked list - reverse linked list
Linked list - reverse linked list
2022-07-26 18:25:00 【Morita Rinko】
subject :
Given the head node of a single linked list pHead, The length is n, After reversing the linked list , Return the header of the new linked list .
For example, when entering a linked list {1,2,3} when ,
After reversal , The original linked list becomes {3,2,1}, So the corresponding output is {3,2,1}.
The above conversion process is shown in the figure below :

Their thinking :
Reversing the linked list is to make each node next The pointer points back to the previous node , So here's what we can think about , The steps are as follows :
among :
- pre It is used to point to the previous node that the inversion needs to point to , At the beginning pre by nullptr.
- cur It is used to point to the current node .
- next Is used to point to cur Next node of , because cur The node of next If once pointed pre node , So originally cur The next node of will not be found , So we need to use next Point to cur Next node of
Solution code :
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
ListNode* cur=pHead,*pre=nullptr,*nex=nullptr;
// Final cur Point to nullptr,pre Point to the last node , That is, reverse the head node of the linked list
while(cur){
nex=cur->next;
cur->next=pre;
pre=cur;
cur=nex;
}
return pre;
}
};
边栏推荐
- Visual VM 定位OOM,FullGC使用方法
- Meta Cambria手柄曝光,主动追踪+多触觉回馈方案
- 6、 Common commands of ROS (I): rosnode, rostopic, rosmsg
- Baidu PaddlePaddle easydl x wesken: see how to install the "eye of AI" in bearing quality inspection
- J9 number theory: how to avoid the trap of stepping on thunder?
- 老子云携手福昕鲲鹏,首次实现3D OFD三维版式文档的重大突破
- Vector CANoe Menu Plugin拓展入门
- Ten year structure five year life-06 impulse to leave
- How to switch nodejs versions at will?
- Leetcode 0137. number II that appears only once
猜你喜欢

MySQL 遇到过死锁问题吗,你是如何解决的?

ssm练习第四天_获取用户名_用户退出_用户crud_密码加密_角色_权限

LeetCode50天刷题计划(Day 5—— 最长回文子串 10.50-13:00)

It is said that the salary of Alibaba P7 is really fragrant

Linked list - the penultimate K nodes

Vector CANoe Menu Plugin拓展入门

如何组装一个注册中心

Oracle第一天(开发常用的知识点再回顾整理下)

相对路径与绝对路径

Baidu PaddlePaddle easydl x wesken: see how to install the "eye of AI" in bearing quality inspection
随机推荐
The first day of Oracle (review and sort out the common knowledge points of development)
Online stock trading, where to choose to open an account is safer?
LeetCode 0139. 单词拆分
Point cloud target detection Kitti dataset bin file visualization, one-stop solution
Day 4 of SSM practice_ Get user name_ User exit_ User CRUD_ Password encryption_ Roles_ jurisdiction
剑指offer 跳台阶扩展问题
10、 Implementation of parameter modification of parameter server
What is the PMP exam outline in 2022?
Continue to work hard on your skills, and the more you learn, the more you will learn
Hello World
Leetcode 0137. number II that appears only once
DTS搭载全新自研内核,突破两地三中心架构的关键技术|腾讯云数据库
Privacy computing basic component series - confusion circuit
你适合做自动化 测试吗?
《圆圈正义》的信念
Greedy - 455. Distribute cookies
How about the employment prospects of Russian translation? How to do a good job of Russian translation
“蔚来杯“2022牛客暑期多校训练营3记录
[ Kitex 源码解读 ] 服务发现
LeetCode_1005_K次取反后最大化的数组和