当前位置:网站首页>Question brushing record ---- reverse the linked list (reverse the whole linked list)
Question brushing record ---- reverse the linked list (reverse the whole linked list)
2022-07-28 06:46:00 【HandsomeDog_ L】
Catalog
Title Description : Given a linked list , Invert and return to the new header node
recursive : Reverse from back to front
hold head.next Later nodes are treated as a node
Title Description : Given a linked list , Invert and return to the new header node
iteration :
Two pointers ,pre and cur,cur Traverse the linked list instead of the head node , In the process next Record cur.next
ListNode reverse(ListNode head) {
ListNode pre = null, cur = head;
while (cur != null) {
ListNode next = cur.next;// Record the next node
cur.next = pre;// Change the direction of the first node
pre = cur;// The predecessor node is updated to the current node
cur = next;// Update node backward
}
return pre;
}recursive :
public ListNode reverseList(ListNode head) {
return reverse(null, head);
}
private ListNode reverse(ListNode prev, ListNode cur) {
if (cur == null) {
return prev;
}
ListNode temp = null;
temp = cur.next;// Save the next node first
cur.next = prev;// reverse
// to update prev、cur Location
// prev = cur;
// cur = temp;
return reverse(cur, temp);
}recursive : Reverse from back to front
hold head.next Later nodes are treated as a node
ListNode reverseList(ListNode head) {
// Edge condition judgment
if(head == null) return null;
if (head.next == null) return head;
// Recursively call , Flip the second node and start the subsequent linked list
ListNode last = reverseList(head.next);
// Turn the direction between the head node and the second node
head.next.next = head;
// At this time head The node is the tail node ,next Need to point to NULL
head.next = null;
return last;
} 边栏推荐
- OJ 1284 记数问题
- SSAO by computer shader (I)
- 刷题记录----链表
- [untitled]
- Mongodb quick start
- 江中ACM新生10月26日习题题解
- [dynamic planning -- the best period for buying and selling stocks Series 2]
- NFT data storage blind box + mode system development
- ZOJ Problem 1005 jugs
- [dynamic planning -- the best period series for buying and selling stocks]
猜你喜欢

【无标题】

Graphic pipeline foundation (II)

用c语言实现三子棋小游戏

Leetcode brush questions diary sword finger offer II 047. Binary tree pruning

Leetcode 刷题日记 剑指 Offer II 047. 二叉树剪枝

SSAO By Computer Shader(二)
![[untitled]](/img/54/660667e528729cc87796d972dc0b17.png)
[untitled]
![[dynamic planning -- the best period series for buying and selling stocks]](/img/90/9060eabc9b9e7f660504806fde282d.png)
[dynamic planning -- the best period series for buying and selling stocks]

【C语言】字符串库函数介绍及模拟

Leetcode brush question diary sword finger offer II 048. serialization and deserialization binary tree
随机推荐
【C语言】动态内存管理
Leetcode 刷题日记 剑指 Offer II 050. 向下的路径节点之和
Project compilation nosuch*** error problem
SSAO by computer shader (I)
feignclient @RequestMapping参数设置及请求头简易方式设置
【自我救赎的开始】
关于时间复杂度,你不知道的都在这里
RayMarching实现体积光渲染
Water drop effect on umbrella
What's a good gift for your girlfriend on the Chinese Valentine's day in 2022? Practical and beautiful gift recommendation
OJ 1018 报数游戏
Leetcode brush question diary sword finger offer II 053. Medium order successor in binary search tree
从普通查询商品到高并发查询商品的优化思路
Execjs call
AQS之CyclicBarrier源码解析
【实现简易版扫雷小游戏】
[dynamic planning -- the best period for buying and selling stocks Series 2]
[untitled]
Battle plague Cup -- my account book
中国剩余定理 个人理解