当前位置:网站首页>206. reverse linked list
206. reverse linked list
2022-07-01 04:05:00 【Sit at a sunny window and drink tea alone】

Train of thought Change pointer direction

Main idea : Traverse all nodes , Change the direction of the node
- The initial state : last Point to null, head Point to the head node

- Define a nextHead node , Used to store head.next The node of , Otherwise, once the direction changes , head.next The node is lost

- Then change head Direction of edges of , head.next = last

- last and head Move forward one step

- Then cycle the above process
class Solution {
public ListNode reverseList(ListNode head) {
ListNode last = null;
while(head != null) {
// preservation head.next , as a result of When the direction of the edge changes , head.next Nodes of will be lost , So save it in advance
ListNode nextHead = head.next;
// Change the direction of the edge
head.next = last;
// last and head Move forward one step
last = head;
head = nextHead;
}
return last;
}
}
边栏推荐
猜你喜欢

不同性能测试工具的并发模式

The programmer's girlfriend gave me a fatigue driving test
[today in history] June 30: von Neumann published the first draft; The semiconductor war in the late 1990s; CBS acquires CNET

创新界,聚势行 | 2022人大金仓“百城巡展”火热开启

HoloLens2开发环境搭建及部署app
![[TA frost wolf \u may- hundred talents plan] 1.2.2 matrix calculation](/img/49/173b1f1f379faa28c503165a300ce0.png)
[TA frost wolf \u may- hundred talents plan] 1.2.2 matrix calculation

NFT:使用 EIP-2981 开启 NFT 版税之旅
![[ta - Frost Wolf May - 100 people plan] 1.2.1 base vectorielle](/img/94/99090ea91082a385968e071ef3766c.png)
[ta - Frost Wolf May - 100 people plan] 1.2.1 base vectorielle

JMeter login failure, extracting login token, and obtaining token problem solving

【TA-霜狼_may-《百人計劃》】2.3 常用函數介紹
随机推荐
In the innovation community, the "100 cities Tour" of the gold warehouse of the National People's Congress of 2022 was launched
[TA frost wolf _may - "hundred people plan"] 1.4 introduction to PC mobile phone graphics API
Hololens2 development environment building and deploying apps
Embedded System Development Notes 81: Using Dialog component to design prompt dialog box
NFT:使用 EIP-2981 開啟 NFT 版稅之旅
互联网行业最佳产品开发流程 推荐!
[TA frost wolf \u may- hundred people plan] 2.3 introduction to common functions
283. move zero
Unity's 3D multi-point arrow navigation
Visit the image URL stored by Alibaba cloud to preview the thumbnail directly on the web page instead of downloading it directly
LetCode 1829. Maximum XOR value per query
[TA frost wolf \u may- hundred people plan] 2.4 traditional empirical lighting model
Access denied for user ‘ODBC‘@‘localhost‘ (using password: NO)
165. 比较版本号
Millet College wechat scanning code login process record and bug resolution
【人话版】WEB3黑暗森林中的隐私博弈
This may be your last chance to join Tencent
PageObject模式解析及案例
30. Concatenate substrings of all words
[today in history] June 30: von Neumann published the first draft; The semiconductor war in the late 1990s; CBS acquires CNET