反转链表

来源:互联网 发布:上海美猴网络骗局 编辑:程序博客网 时间:2024/06/03 20:40

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

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


0 0
原创粉丝点击