码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 206. Reverse Linked List

时间:2020-02-14 10:27:41      阅读:48      评论:0      收藏:0      [点我收藏+]

标签:solution   node   def   else   code   ems   problems   init   列表   

题目

翻转列表

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

LeetCode 206. Reverse Linked List

标签:solution   node   def   else   code   ems   problems   init   列表   

原文地址:https://www.cnblogs.com/dacc123/p/12306334.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!