LeetCode 206. Reverse Linked List
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*t1, *t2, *t3;
t1=head; t2=t1->next; t3=t2->next;
t1->next=NULL;
while(t3!=NULL){
t2->next=t1;
t1=t2; t2=t3; t3=t3->next;
}
t2->next=t1;
return t2;
}
};