206. 反转链表(递归)
Posted baboon
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了206. 反转链表(递归)相关的知识,希望对你有一定的参考价值。
题目描述
leetcode - 206:https://leetcode-cn.com/problems/reverse-linked-list/
解题关键
- 链表
- 递归
代码
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* ans;
void dfs(ListNode* pre,ListNode* net){
if(net==NULL){
ans = pre;
return;
}
dfs(net,net->next);
net->next=pre;
}
ListNode* reverseList(ListNode* head) {
if(head == NULL) return ans;
dfs(head,head->next);
head->next=NULL;
return ans;
}
};
以上是关于206. 反转链表(递归)的主要内容,如果未能解决你的问题,请参考以下文章