当前位置:网站首页>206.反转链表
206.反转链表
2022-07-01 03:28:00 【兀坐晴窗独饮茶】

思路一 改变指针方向

主体思路 :遍历所有节点, 改变节点的指向方向
- 初始状态 : last 指向 null, head 指向头结点

- 定义一个 nextHead 节点, 用于存储 head.next 的节点, 否则方向一改变, head.next 节点就丢失了

- 然后改变 head的边的方向 , head.next = last

- last 和 head 向前移动一步

- 然后循环上述过程即可
class Solution {
public ListNode reverseList(ListNode head) {
ListNode last = null;
while(head != null) {
// 保存head.next , 原因是 边的方向改变时, head.next 的节点会丢失, 所以要提前保存
ListNode nextHead = head.next;
// 改变边的方向
head.next = last;
// last 和 head 向前移动一步
last = head;
head = nextHead;
}
return last;
}
}
边栏推荐
- 205. isomorphic string
- LeetCode 31下一个排列、LeetCode 64最小路径和、LeetCode 62不同路径、LeetCode 78子集、LeetCode 33搜索旋转排序数组(修改二分法)
- [deep learning] activation function (sigmoid, etc.), forward propagation, back propagation and gradient optimization; optimizer. zero_ grad(), loss. backward(), optimizer. Function and principle of st
- [ta - Frost Wolf May - 100 people plan] 1.2.1 base vectorielle
- 171. excel table column No
- 快速筛选打卡时间日期等数据:EXCEL筛选查找某一时间点是否在某一时间段内
- Binary tree god level traversal: Morris traversal
- Addition without addition, subtraction, multiplication and division
- Blueprism registration, download and install -rpa Chapter 1
- MFC窗口滚动条用法
猜你喜欢

LeetCode 128最长连续序列(哈希set)

Usage of AfxMessageBox and MessageBox

【TA-霜狼_may-《百人计划》】1.2.1 向量基础

Blueprism registration, download and install -rpa Chapter 1

Feature pyramid networks for object detection

【TA-霜狼_may-《百人計劃》】1.2.1 向量基礎

FCN全卷積網絡理解及代碼實現(來自pytorch官方實現)

Implement pow (x, n) function

MFC窗口滚动条用法

【TA-霜狼_may-《百人计划》】2.3 常用函数介绍
随机推荐
[TA frost wolf _may - "hundred people plan"] 1.4 introduction to PC mobile phone graphics API
[JPCs publication] the Third International Conference on control theory and application in 2022 (icocta 2022)
【TA-霜狼_may-《百人计划》】1.2.1 向量基础
Future of NTF and trends in 2022
[reach out to Party welfare] developer reload system sequence
Leetcode:829. 连续整数求和
[TA frost wolf \u may- hundred talents plan] 1.2.3 MVP matrix operation
【EI检索】2022年第六届材料工程与先进制造技术国际会议(MEAMT 2022)重要信息会议网址:www.meamt.org会议时间:2022年9月23-25日召开地点:中国南京截稿时间:2
LeetCode 31下一个排列、LeetCode 64最小路径和、LeetCode 62不同路径、LeetCode 78子集、LeetCode 33搜索旋转排序数组(修改二分法)
Leetcode:829. Sum of continuous integers
205. 同构字符串
[small sample segmentation] interpretation of the paper: prior guided feature enrichment network for fee shot segmentation
Cygwin的下载和安装配置
Appium自动化测试基础--补充:C/S架构和B/S架构说明
242. valid Letter heteronyms
Online public network security case nanny level tutorial [reaching out for Party welfare]
复习专栏之---消息队列
Random seed torch in deep learning manual_ seed(number)、torch. cuda. manual_ seed(number)
What happens when a function is called before it is declared in C?
4. [WebGIS practice] software operation chapter - data import and processing