您的位置:首页 > 其它

反转链表

2016-04-09 12:23 323 查看

题目描述

输入一个链表,反转链表后,输出链表的所有元素。

思路

很简单的模拟,据说链表相关的题目考的是基本功扎不扎实~

/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
ListNode *last = NULL, *now = pHead, *next = NULL;
while (now != NULL) {
next = now->next;
now->next = last;
last = now;
now = next;
}

return last;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: