206. Reverse Linked List

Problem:blog

Reverse a singly linked list.it

 

recursion:io

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if(head == NULL || head->next == NULL) return head;
        ListNode* p = head->next;
        ListNode* n = reverseList(p);
        
        head->next  = NULL;
        p->next     = head;
        
        return n;
    }
};
相關文章
相關標籤/搜索