Title
Reverse a singly linked list.
反转链表
Solution
//cpp:
/**
* 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 *p1=NULL;
while(head){
ListNode *p=new ListNode(head->val);
p->next=p1;
p1=p;
head=head->next;
}
return p1;
}
};