反转链表

来源:互联网 发布:数据挖掘兴起于哪一年 编辑:程序博客网 时间:2024/05/18 03:34

题目描述

输入一个链表,反转链表后,输出链表的所有元素。
/*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;        if(pHead->next==NULL)            return pHead;        ListNode* p=NULL;        ListNode* L=pHead;        ListNode* q=pHead;        while(q!=NULL){            L=q->next;            q->next=p;            p=q;            q=L;        }        return p;    }};


0 0
原创粉丝点击