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) {
if(head==NULL||head->next==NULL)
return head;
ListNode* l1=NULL;
ListNode* l2=head;
while(l2)
{
ListNode* tmp=l2->next;
l2->next=l1;
l1=l2;
l2=tmp;
}
return l1;
}
};leetcode No206. Reverse Linked List
原文:http://blog.csdn.net/u011391629/article/details/52164389