// 鏈表節點結構
struct ListNode {
ListNode(int a = 0) : val(a),
next(nullptr) {
}
int val;
ListNode* next;
};
// 遞歸版本的鏈表反轉
ListNode* ReverseList(ListNode* head) {
if (nullptr == head)
return nullptr;
if (nullptr == head->next)
return head;
ListNode* newHead = ReverseList(head->next);
head->next->next = head;
return newHead;
}
int main(int argc, char* argv[]) {
ListNode* head = new ListNode(1);
head->next = new ListNode(2);
head->next->next = new ListNode(3);
head->next->next->next = new ListNode(4);
ListNode *newHead = ReverseList(head);
return 0;
}