Reverse a singly linked list.
[leetcode url]
https://leetcode.com/problems/reverse-linked-list/description/
- 递归实现
/**
* 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 == nullptr) return head;
if (head -> next == nullptr) return head;
ListNode *tail= reverseList(head->next);
head->next->next = head; // 翻转了指针
head ->next = nullptr; // head指向null
return tail;
}
};
- 非递归实现:
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode *prev = NULL;
/*
重复做下面操作:
pre -> curr (head) -> next;
pre <- curr (head) -> next;
*/
ListNode *pCurr = pHead;
ListNode *pReverseHead = NULL;
ListNode * pPrev = NULL;
while (pCurr != NULL){
ListNode * pNext = pCurr->next;
if (pNext == NULL)
pReverseHead = pCurr;
pCurr -> next = pPrev;
pPrev = pCurr;
pCurr = pNext;
}
return pReverseHead;
}