将链表里面的数字取出来,再倒着放回去就好了
/**
-
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)
{
ListNode* tem=new ListNode(sizeof(ListNode));
ListNode* t=tem;vector<int>cc; while(head!=NULL) { cc.push_back(head->val); head=head->next; } for(int i=cc.size()-1;i>=0;i--) { ListNode* ret=new ListNode(sizeof(ListNode)); ret->val=cc[i]; t->next=ret; t=t->next; } t->next=NULL; return tem->next;
}
};
来源:CSDN
作者:qq_40742888
链接:https://blog.csdn.net/qq_40742888/article/details/104630120