当前位置:网站首页>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
- 【快捷键】
- 【TA-霜狼_may-《百人计划》】1.2.3 MVP矩阵运算
- 8. 字符串转换整数 (atoi)
- Appium automation test foundation -- supplement: c/s architecture and b/s architecture description
- 5. [WebGIS practice] software operation - service release and permission management
- 318. 最大单词长度乘积
- 242. 有效的字母异位词
- 72. 编辑距离
- IPv4 and IPv6, LAN and WAN, gateway, public IP and private IP, IP address, subnet mask, network segment, network number, host number, network address, host address, and IP segment / number - what does
猜你喜欢
Implement pow (x, n) function
[TA frost wolf \u may- hundred people plan] 2.4 traditional empirical lighting model
Error: plug ins declaring extensions or extension points must set the singleton directive to true
[TA frost wolf \u may- hundred people plan] 1.2.1 vector basis
【TA-霜狼_may-《百人计划》】2.2 模型与材质空间
FCN全卷積網絡理解及代碼實現(來自pytorch官方實現)
5. [WebGIS practice] software operation - service release and permission management
The programmer's girlfriend gave me a fatigue driving test
Idea plug-in backup table
报错:Plug-ins declaring extensions or extension points must set the singleton directive to true
随机推荐
Addition without addition, subtraction, multiplication and division
Blueprism registration, download and install -rpa Chapter 1
389. 找不同
[ta - Frost Wolf May - 100 people plan] 1.2.1 base vectorielle
[small sample segmentation] interpretation of the paper: prior guided feature enrichment network for fee shot segmentation
谷粒学院微信扫码登录过程记录以及bug解决
Pyramid Scene Parsing Network【PSPNet】论文阅读
10. 正则表达式匹配
The combination of applet container technology and IOT
You cannot right-click F12 to view the source code solution on the web page
Leetcode 128 longest continuous sequence (hash set)
242. 有效的字母异位词
[party benefits] jsonobject to string, leave blank
171. excel table column No
Pytorch training deep learning network settings CUDA specified GPU visible
报错:Plug-ins declaring extensions or extension points must set the singleton directive to true
409. longest palindrome
AfxMessageBox和MessageBox的用法
166. fractions to decimals
Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products