LeetCode: 反转链表

题目:
给你单链表的头节点 head ,请你反转链表,并返回反转后的链表
在这里插入图片描述
在这里插入图片描述

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        return reverse(nullptr,head);
    }
     ListNode* reverse(ListNode* pre,ListNode*cur)
     {
         if(cur == nullptr) return pre;
         ListNode*tmp = cur->next;
         cur->next = pre;
         return reverse(cur,tmp);
     }
};
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode*pre = nullptr;
        ListNode*cur = head;
        ListNode*tmp ;

        while(cur)
        {
            tmp = cur->next;
            cur->next = pre;
            pre = cur;
            cur = tmp;
        }
        return pre;
    }
};
posted @ 2021-10-19 16:01  simonlma  阅读(16)  评论(0)    收藏  举报