反转链表

来源:互联网 发布:域名一般买几年 编辑:程序博客网 时间:2024/06/08 01:40

题目描述

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

思路

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

/*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;    }};
0 0
原创粉丝点击