当前位置:网站首页>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;
}
};
边栏推荐
- Day 4 of SSM practice_ Get user name_ User exit_ User CRUD_ Password encryption_ Roles_ jurisdiction
- 数据库索引的原理,为什么要用 B+树,为什么不用二叉树?
- .net CLR GC dynamic loading transient heap threshold calculation and threshold excess calculation
- 剑指offer 连续子数组的最大和(二)
- MySQL 遇到过死锁问题吗,你是如何解决的?
- 链表-合并两个排序的列表
- Redis持久化RDB/AOF
- LeetCode 0137. 只出现一次的数字 II
- What is the PMP exam outline in 2022?
- 网易游戏研发工程师实习生(客户端方向)一面
猜你喜欢
![[Digital IC] understand Axi Lite protocol in simple terms](/img/2b/15b3d831bba6aa772ad83f3ac91d23.png)
[Digital IC] understand Axi Lite protocol in simple terms

ICML 2022(第四篇)|| 图分层对齐图核实现图匹配

隐私计算基础组件系列-混淆电路

Leetcode 50 day question brushing plan (day 1 - add two numbers 11.00-12.30)

LeetCode50天刷题计划(Day 1—— 两数相加 11.00-12.30)

J9数字论:如何避免踩雷多头陷阱?

Sign up now | cloud native technology exchange meetup Guangzhou station has been opened, and I will meet you on August 6!

你适合做自动化 测试吗?

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

Bulletgraph (bullet diagram, bullet diagram)
随机推荐
Laozi cloud and Fuxin Kunpeng achieved a major breakthrough in 3D ofd 3D format documents for the first time
网易游戏研发工程师实习生(客户端方向)一面
Are you suitable for automated testing?
China polyisobutylene Market Research and investment value report (2022 Edition)
2020美亚个人赛复盘
相对路径与绝对路径
How to assemble a registry
Online stock trading, where to choose to open an account is safer?
Leetcode 50 day question brushing plan (day 4 - longest palindrome substring 14.00-16:20)
Quartz trigger rule
OpenWrt之feeds.conf.default详解
Detailed explanation of openwrt's feeds.conf.default
LeetCode_134_加油站
Hello World
DTS搭载全新自研内核,突破两地三中心架构的关键技术|腾讯云数据库
Linux Installation mysql8.0.29 detailed tutorial
PS_1_认识主界面_新建文档(分辨率)_打开保存(序列动画)
The faith of circle justice
Sign up now | cloud native technology exchange meetup Guangzhou station has been opened, and I will meet you on August 6!
分布式链路追踪Jaeger在Golang中的使用