Sword Points Offer_Programming Questions_15

Topic description

Input a linked list, after reversing the linked list, output all elements of the linked list.
/*
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;
    }
};

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324768855&siteId=291194637