题目
https://leetcode.cn/problems/fan-zhuan-lian-biao-lcof/
参考资料
https://leetcode.cn/problems/fan-zhuan-lian-biao-lcof/comments/816840
递归总结:
https://leetcode.cn/problems/fan-zhuan-lian-biao-lcof/solution/kan-bu-dong-di-gui-de-kan-guo-lai-xi-wan-1akq/
两种解法:
https://leetcode.cn/problems/fan-zhuan-lian-biao-lcof/solution/jian-zhi-offer-24-fan-zhuan-lian-biao-die-dai-di-2/
思路要点
代码
//思想就是把链表的值存入栈中
//再利用栈的值构建链表
//问题在于我不会构建链表,参考了第一篇题解
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(head==NULL) return head;
stack<int> s;
while(head){
s.push(head->val);
head=head->next;
}
ListNode* ans=new ListNode(s.top());
ListNode* node=ans;
s.pop();
while(!(s.empty())){
ans->next=new ListNode(s.top());
s.pop();
ans=ans->next;
}
return node;
}
};
递归写法 找个时间对递归总结一下
struct ListNode* reverseList(struct ListNode* head){
if(head==NULL|| head->next==NULL) return head;
struct ListNode* node=reverseList(head->next);
head->next->next=head;
head->next=NULL;
return node;
}