16 反转单链表

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

C++:

 1 /*
 2 struct ListNode {
 3     int val;
 4     struct ListNode *next;
 5     ListNode(int x) :
 6        val(x), next(NULL) {
 7     }
 8 };*/
 9 class Solution {
10 public:
11     ListNode* ReverseList(ListNode* pHead) {
12         ListNode* p = NULL ;
13         while(pHead != NULL){
14             ListNode* pNext = pHead->next ;
15             pHead->next = p ;
16             p = pHead ;
17             pHead = pNext ;
18         }
19         return p ;
20     }
21 };

猜你喜欢

转载自www.cnblogs.com/mengchunchen/p/8933968.html