题目描述:反转一个单链表。
题目链接:206. 反转链表
/**
* 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==NULL) return NULL;
ListNode* ans;
if (head->next!=NULL){
ans=reverseList(head->next);
head->next->next=head;
}
else ans=head;
head->next=NULL;
return ans;
}
};