206. Reverse Linked List

Problem:

Reverse a singly linked list.

 

recursion:

/**
 * 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;
    }
};

  

 

posted @ 2016-06-06 10:05  liu_ty10  阅读(124)  评论(0编辑  收藏  举报