题目描述
单链表原地逆置
样例
blablabla
算法1
C++ 代码
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
struct Node
{
int val;
Node* next;
Node(int _val):val(_val),next(NULL){}
};
Node* reverseList(Node* head)
{
if(!head || !head->next) return head;
Node* pre = NULL,*cur = head;
while(cur)
{
Node* post = cur->next;
cur->next = pre;
pre = cur;
cur = post;
}
return pre;
}
int main()
{
//\\\\
reverseList(head);
return 0;
}