【链表】链表的逆序【Add to List 206. Reverse Linked List】

来源:互联网 发布:大数据研究方向有哪些 编辑:程序博客网 时间:2024/06/14 03:58

题目链接:https://leetcode.com/problems/reverse-linked-list/#/description

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     ListNode *next; *     ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:    ListNode* t,*last;    ListNode* reverseList(ListNode* head) {        //  空链表||访问到链表尾部        if(!head||!(head->next)){            last=head;  //  记录尾部节点            return head;        }        //  t表示reverse后的结果        t=reverseList(head->next);                last->next=head;        head->next=NULL;        last=head;        return t;    }};