刷题记录第一天:链表ACM模式+反转链表
#include<iostream>
using namespace std;
struct ListNode{
int val;
ListNode* next;
ListNode(int x): val(x), next(nullptr){}
};
ListNode* init(){
ListNode* head = new ListNode(5);
ListNode* node1 = new ListNode(4);
ListNode* node2 = new ListNode(3);
ListNode* node3 = new ListNode(2);
ListNode* node4 = new ListNode(1);
head->next = node1;
node1->next = node2;
node2->next = node3;
node3->next = node4;
node4->next =nullptr;
return head;
}
void print_list(ListNode* head){
while(head!=nullptr){
cout<<head->val<<" ";
head = head->next;
}
}
ListNode* reverse(ListNode* head){
ListNode* current = head;
ListNode* pre = nullptr;
while(current!=nullptr){
ListNode* next = current->next;
current->next = pre;
pre = current;
current = next;
}
return pre;
}
int main(){
ListNode* head = init();
print_list(reverse(head));
system("pause");
};