LeetCode 206. Reverse Linked List

题目

翻转列表

/**
 * 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* term = head->next;
        head->next=NULL;
        return Fun(term,head);
        
    }
    
    ListNode* Fun(ListNode* head,ListNode* pre)
    {
        
        ListNode* term = head->next;
        
        head->next = pre;
        
        if(term!=NULL)
            return  Fun(term,head);
        else
            return head;
    }
};
原文地址:https://www.cnblogs.com/dacc123/p/12306334.html