llllmz

导航

206. 反转链表

 

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if(!head) return nullptr;
        ListNode* cur = head->next;
        if(!cur) return head;
        head->next = nullptr;
        while(cur){
            ListNode* temp = cur->next;
            cur->next = head;
            head = cur;
            cur = temp;
        }
        return head;
    }
};

posted on 2024-09-24 18:48  神奇的萝卜丝  阅读(10)  评论(0)    收藏  举报