Skip to content

E206.反转链表

three pinters, recursion, https://leetcode.cn/problems/reverse-linked-list/

思路:直接写就行,0ms,一遍过,用时约10min

cpp
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* prev = nullptr;
        ListNode* cur = head;
        while (cur) 
        {
            ListNode* nxt = cur->next;
            cur->next = prev;
            prev = cur;
            cur = nxt;
        }
        head = prev;
        return head;
    }
};