剑指Offer_编程题_15

题目描述

输入一个链表,反转链表后,输出链表的所有元素。
/*
struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};*/
class Solution {
public:
    ListNode* ReverseList(ListNode* pHead) {
        ListNode* head = pHead;
        ListNode* p1 = pHead;
        ListNode* p2 = pHead;
        head = NULL;
        while(p1){
            p2 = p1->next;
            p1->next = head;
            head = p1;
            p1=p2;
        }
        return head;
    }
};

  

原文地址:https://www.cnblogs.com/grglym/p/8931200.html