206. Reverse Linked List

Reverse a singly linked list.

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if(head==NULL||head->next==NULL)
           return head;
        ListNode* pre = head;
        ListNode* cur = head->next;
        ListNode* next = head->next->next;
        pre->next = NULL;
        
        while(cur)
        {
            next = cur->next;
            cur->next = pre;
            pre = cur;
            cur = next;
        }
        return pre;
    }
};

你可能感兴趣的:(206. Reverse Linked List)