AcWing 35. 反转链表
原题链接
简单
作者:
飞呀
,
2021-05-01 23:42:32
,
所有人可见
,
阅读 343
/**
* 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) return head;
ListNode* cur = head;
stack<ListNode*> node;
while(cur != NULL){
node.push(new ListNode(cur->val));
cur = cur->next;
}
head = node.top();
node.pop();
cur = head;
while(!node.empty()){
cur->next = node.top();
cur = cur->next;
node.pop();
}
return head;
}
};