206. Reverse Linked List

Posted Lee

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
    }
};

  

 

以上是关于206. Reverse Linked List的主要内容,如果未能解决你的问题,请参考以下文章

206. Reverse Linked List

206. Reverse Linked List

206. Reverse Linked List

206. Reverse Linked List

No 206, Reverse Linked List

206. Reverse Linked List