当前位置:网站首页>35. Reverse linked list
35. Reverse linked list
2022-07-31 00:58:00 【Hunter_Kevin】
题目
定义一个函数,输入一个链表的头结点,反转该链表并输出反转后链表的头结点.
思考题:
请同时实现迭代版本和递归版本.
数据范围
链表长度 [0,30].
样例
输入:1->2->3->4->5->NULL
输出:5->4->3->2->1->NULL
Iterative version code 1
Traverse from the first node and the second node to the next node of the tail node,
Initially, the predecessor pointer points to the first node of the linked list
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(!head || !head->next) return head;//如果链表为空或者只有一个节点,则不需要反转
ListNode* a = head, *b = head->next;//a为第一个节点,b为第二个节点
while(b){
//等价于a->next
auto c = b->next;//临时保存b的下一个节点
b->next = a;//b的next指针指向a
a = b, b = c;//a和b往后移动一个节点
}
head->next = NULL;//For the reversed tail nodenext指针置为NULL;
return a;//aThis is the inverted head node
}
};
Iterative version code 2
Traverse from the head node to the tail node,
Initially, the predecessor pointer points to null
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode * pre = nullptr;
ListNode * cur = head;
while(cur){
//当curTraverse to the end of the linked list
auto next = cur->next;//临时保存cur的下一个节点地址
cur->next = pre;//Update the pointer of the current node
pre = cur;//The predecessor node is moved to the current node
cur = next;//当前节点移动到cur的下一个节点
}
return pre;//Returns the predecessor node of the original linked list tail node,That is, the head node after reversing the linked list
}
};
递归版代码
- The key is to take advantage of the recursion end condition,Controlling the deepest recursion is at the tail nodereturn尾节点地址
- Because the recursive parameter is
head->next
,so immediately5->next==null
满足递归结束条件head->next == null
When exiting to the previous level of recursion,,head为尾节点5的前驱节点4- Execute two more lines of critical code to modify the node pointer
head->next->next = head;
The purpose is through accesshead节点4,修改节点4的后继节点5的next指针指向节点4head->next = null;
目的是让当前head节点4指向空- Because the return value of the previous recursive function call is always the one when the deepest recursion exits
head->next==null
,That is, the tail node of the original linked list,一直没有发生变化
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(!head || !head->next)return head;//如果链表为空,Or satisfy the recursion end conditionhead->next==null
auto res = reverseList(head->next);//Save the head node returned to the final linked list,The return value is always the head node of the final linked list
head->next->next = head;//When the deepest level recursively exits,即5->next == null时退出,The recursion end condition returns directlyhead,即res = head==5
//At this point, it returns to the recursion of the previous levelhead为4,The purpose of this line of code is to make 5的next指针指向4
head->next = nullptr;//Let the current node point to null
return res;//Returns the head node of the final linked list that has been saved,即head==5
}
};
边栏推荐
猜你喜欢
VS warning LNK4099:未找到 PDB 的解决方案
ShardingSphere's unsharded table configuration combat (6)
分布式.幂等性
华为“天才少年”稚晖君又出新作,从零开始造“客制化”智能键盘
Oracle has a weird temporary table space shortage problem
typescript13-类型别名
蓝牙mesh系统开发二 mesh节点开发
typescript16-void
Jmeter参数传递方式(token传递,接口关联等)
ShardingSphere's public table combat (7)
随机推荐
Typescript18 - object type
射频器件的基本参数1
一万了解 Gateway 知识点
Sping.事务的传播特性
typescript16-void
The client series of the DOM series
人工智能与云安全
ros2知识:在单个进程中布置多个节点
GO GOPROXY代理设置
Thesis understanding: "Designing and training of a dual CNN for image denoising"
Jetpack Compose learning (8) - State and remeber
Mysql systemized JOIN operation example analysis
Mini Program - Global Data Sharing
网站频繁出现mysql等数据库连接失败等信息解决办法
ShardingSphere之读写分离(八)
剑指offer17---打印从1到最大的n位数
Typescript14 - (type) of the specified parameters and return values alone
ELK部署脚本---亲测可用
深度学习可以求解特定函数的参数么?
查看zabbix-release-5.0-1.el8.noarch.rpm包内容