leetcode Reverse Linked List

题目连接

https://leetcode.com/problems/reverse-linked-list/  

Reverse Linked List

Description

Reverse a singly linked list.

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
	ListNode* reverseList(ListNode *head) {
		ListNode *A = head, *B = NULL;
		while (head) {
			ListNode *ret = head;
			head = head->next;
			A->next = B;
			B = ret;
			A = head;
		}
		return B;
	}
};
原文地址:https://www.cnblogs.com/GadyPu/p/5028165.html