题目
输入一个链表,反转链表后,输出链表的所有元素。
分析
反转链表只需改变链接方向,改变方向时需要将原本指向后一个结点的链接方向指向前一个结点,因此需要记录下三个结点。
github链接:JZ15-反转链表
C++代码
#include <iostream>
using namespace std;
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
if(pHead == NULL){
return NULL;
}
ListNode* front = pHead;
ListNode* rear = front->next;
front->next = NULL;
ListNode* tmp;
while(rear){
tmp = rear->next;
rear->next = front;
front = rear;
rear = tmp;
}
return front;
}
};
int main(){
int n;
while(cin>>n){
ListNode* head = new ListNode(0);
ListNode* tmp = head;
int v;
for(int i = 0 ; i < n ; i++){
cin>>v;
ListNode* node = new ListNode(v);
head->next = node;
head = head->next;
}
head = tmp->next;
tmp = head;
while(tmp){
cout<<tmp->val<<" ";
tmp = tmp->next;
}
cout<<endl;
Solution s;
tmp = s.ReverseList(head);
while(tmp){
cout<<tmp->val<<" ";
tmp = tmp->next;
}
}
return 0;
}