当前位置:网站首页>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;
}
}
边栏推荐
- Leetcode:829. 连续整数求和
- 【JPCS出版】2022年第三届控制理论与应用国际会议(ICoCTA 2022)
- torch. histc
- 214. minimum palindrome string
- 168. Excel表列名称
- Pyramid scene parsing network [pspnet] thesis reading
- [TA frost wolf \u may- hundred people plan] 2.4 traditional empirical lighting model
- 谷粒学院微信扫码登录过程记录以及bug解决
- Jeecgboot output log, how to use @slf4j
- 205. 同构字符串
猜你喜欢

Error: plug ins declaring extensions or extension points must set the singleton directive to true

Pyramid Scene Parsing Network【PSPNet】论文阅读

整合阿里云短信的问题:无法从静态上下文中引用非静态方法

Review column - message queue

Use selenium automated test tool to climb the enrollment score line and ranking of colleges and universities related to the college entrance examination

Cygwin的下载和安装配置

程序员女友给我做了一个疲劳驾驶检测

TEC: Knowledge Graph Embedding with Triple Context

用小程序的技术优势发展产业互联网

Grid system in bootstrap
随机推荐
242. valid Letter heteronyms
[小样本分割]论文解读Prior Guided Feature Enrichment Network for Few-Shot Segmentation
TEC: Knowledge Graph Embedding with Triple Context
Test function in pychram
【TA-霜狼_may-《百人计划》】2.2 模型与材质空间
Leetcode 31 next spread, leetcode 64 minimum path sum, leetcode 62 different paths, leetcode 78 subset, leetcode 33 search rotation sort array (modify dichotomy)
389. find a difference
Appium自动化测试基础 — APPium基本原理
241. Design priorities for operational expressions
72. 编辑距离
Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products
How keil displays Chinese annotations (simple with pictures)
LeetCode 128最长连续序列(哈希set)
Are you still wasting brain cells for self-study? This interview note is definitely the ceiling of station C
【TA-霜狼_may-《百人计划》】1.2.3 MVP矩阵运算
242. 有效的字母异位词
Future of NTF and trends in 2022
[TA frost wolf \u may- hundred people plan] 1.3 secret of texture
72. edit distance
TEC: Knowledge Graph Embedding with Triple Context