经典指数          
原因
1677
浏览数
0
收藏数
 

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

     举报   纠错  
 
切换
1 个答案

简单模拟题。。。这种问题代码越简单越清晰吧。。不要写太复杂了。。

/*

struct ListNode {

int val;

struct ListNode *next;

ListNode(int x) :

val(x), next(NULL) {

}

};*/

class Solution {

public:

    ListNode* ReverseList(ListNode* pHead) {

ListNode* h = NULL;

        for(ListNode* p = pHead; p; ){

            ListNode* tmp = p -> next;

            p -> next = h;

            h = p;

            p = tmp;

        }

        return h;

    }

};

 
切换
撰写答案