题目链接: 反转链表
有关题目
给你单链表的头节点 head ,请你反转链表,
并返回反转后的链表。
示例 3:
输入:head = []
输出:[]
提示:
链表中节点的数目范围是 [0, 5000]
-5000 <= Node.val <= 5000
进阶:链表可以选用迭代或递归方式完成反转。
你能否用两种方法解决这道题?
题解
法一:栈
使用栈先进后出性质:
先链表节点按顺序入栈,当全部入栈完之后再按顺序出栈,
出栈的时候在把出栈的结点串成一个新的链表
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
stack<ListNode*> stk;
while(head != nullptr){
stk.push(head);
head = head->next;
}
if (stk.empty()){
return nullptr;
}
ListNode* newHead = stk.top();//拿到栈顶元素
stk.pop();//删除栈顶元素
ListNode* cur = newHead;
while(!stk.empty()){
ListNode* temp = stk.top();
stk.pop();
cur->next = temp;
cur = cur->next;
}
//防止构成环
cur->next = nullptr;
return newHead;
}
};
时间复杂度:O(N)
空间复杂度:O(N),调用栈存放原链表长度
法二:递归
思路一:注意我们正处于nk
思路二:
参考官方题解评论区下NoColor96
以链表1->2->3->4->5举例
假设我们现在到达最后一个节点:
第一轮出栈,head为5,head.next为空,返回5
第二轮出栈,head为4,head.next为5,执行head.next.next=head也就是5.next=4,
把当前节点的子节点的子节点指向当前节点
此时链表为1->2->3->4<->5,由于4与5互相指向,所以此处要断开4.next=null
此时链表为1->2->3->4<-5
返回节点5
第三轮出栈,head为3,head.next为4,执行head.next.next=head也就是4.next=3,
此时链表为1->2->3<->4<-5,由于3与4互相指向,所以此处要断开3.next=null
此时链表为1->2->3<-4<-5
返回节点5
第四轮出栈,head为2,head.next为3,执行head.next.next=head也就是3.next=2,
此时链表为1->2<->3<-4<-5,由于2与3互相指向,所以此处要断开2.next=null
此时链表为1->2<-3<-4<-5
返回节点5
第五轮出栈,head为1,head.next为2,执行head.next.next=head也就是2.next=1,
此时链表为1<->2<-3<-4<-5,由于1与2互相指向,所以此处要断开1.next=null
此时链表为1<-2<-3<-4<-5
返回节点5
出栈完成,最终头节点5->4->3-2->1
思路三:
参考官方题解评论区懒懒米虫
不妨假设链表为1,2,3,4,5。
按照递归,当执行reverseList(5)的时候返回了5这个节点,
reverseList(4)中的newHead就是5这个节点,
我们看看reverseList(4)接下来执行完之后,
5->next = 4, 4->next = null。
这时候返回了newHead这个节点,也就是链表5->4->null,
接下来执行reverseList(3),
代码解析为4->next = 3,3->next = null,
这个时候newHead就变成了,5->4->3->null, reverseList(2), reverseList(1)依次类推,
newHead就是:5->4->3->2->1->null
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
//递归终止条件
if (head == nullptr || head->next == nullptr){
return head;
}
ListNode* newHead = reverseList(head->next);
head->next->next = head;
head->next = nullptr;
return newHead;
}
};
法三:迭代
代码一:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* newHead = nullptr;
ListNode* cur = head;
while(cur){
ListNode* temp = cur;
cur = cur->next;
temp->next = nullptr;
if (newHead == nullptr){
newHead = temp;
}
else {
temp->next = newHead;
newHead = temp;
}
}
return newHead;
}
};
时间复杂度:O(N)
空间复杂度:O(1)
代码二:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* cur = head;
ListNode* p = nullptr;
while(cur){
ListNode* temp = cur->next;
cur->next = p;
p = cur;
cur = temp;
}
return p;
}
};
时间复杂度:O(N)
空间复杂度:O(1)