当前位置:网站首页>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;
}
}
边栏推荐
- Feature pyramid networks for object detection
- Complete knapsack problem
- C语言的sem_t变量类型
- pytorch nn. AdaptiveAvgPool2d(1)
- [TA frost wolf \u may- hundred people plan] 1.2.1 vector basis
- [small sample segmentation] interpretation of the paper: prior guided feature enrichment network for fee shot segmentation
- Download and installation configuration of cygwin
- 【EI会议】2022年国际土木与海洋工程联合会议(JCCME 2022)
- PageObject模式解析及案例
- MFC窗口滚动条用法
猜你喜欢

Are you still wasting brain cells for self-study? This interview note is definitely the ceiling of station C

Use of comment keyword in database

【TA-霜狼_may-《百人计划》】1.2.2 矩阵计算

Appium自动化测试基础 — APPium基本原理
![Pyramid scene parsing network [pspnet] thesis reading](/img/05/4645c8a595083479dee6835620335d.png)
Pyramid scene parsing network [pspnet] thesis reading

Idea plug-in backup table

pytorch nn. AdaptiveAvgPool2d(1)

Pytorch training deep learning network settings CUDA specified GPU visible

TEC: Knowledge Graph Embedding with Triple Context

Quickly filter data such as clock in time and date: Excel filter to find whether a certain time point is within a certain time period
随机推荐
衡量两个向量相似度的方法:余弦相似度、pytorch 求余弦相似度:torch.nn.CosineSimilarity(dim=1, eps=1e-08)
The problem of integrating Alibaba cloud SMS: non static methods cannot be referenced from the static context
Idea plug-in backup table
整合阿里云短信的问题:无法从静态上下文中引用非静态方法
Leetcode:剑指 Offer 59 - I. 滑动窗口的最大值
程序员女友给我做了一个疲劳驾驶检测
[TA frost wolf \u may- hundred talents plan] 1.2.2 matrix calculation
208. 实现 Trie (前缀树)
[TA frost wolf \u may- hundred people plan] 2.3 introduction to common functions
TEC: Knowledge Graph Embedding with Triple Context
Use selenium automated test tool to climb the enrollment score line and ranking of colleges and universities related to the college entrance examination
Millet College wechat scanning code login process record and bug resolution
171. Excel 表列序号
168. excel table column name
318. Maximum word length product
431. 将 N 叉树编码为二叉树 DFS
242. 有效的字母异位词
Use of comment keyword in database
392. 判断子序列
187. repeated DNA sequences