6. Reverse Linked List 逆转单链表

逆转单链表,比较简单,不细讲,扫描依次改变指针指向。

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if(head==nullptr)return head;
        ListNode * tmp = head->next;
        ListNode *prenode = head;
        while(tmp!=NULL)
        {  
            ListNode *nextnode = tmp->next;
            tmp->next = prenode;
            prenode = tmp;
            tmp = nextnode;
         }
         head->next = NULL;
         head = prenode;
         return head;
    }
};

你可能感兴趣的:(6. Reverse Linked List 逆转单链表)