题目描述
输入一个链表,反转链表后,输出链表的所有元素
/* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { } };*/ class Solution { public: ListNode* ReverseList(ListNode* pHead) { if(NULL==pHead || NULL==pHead->next) return pHead; ListNode *p,*q,*r; p=pHead; q=pHead->next; pHead->next=NULL; while(q) { r=q->next; q->next=p; p=q; q=r; } pHead=p; return pHead; } };
下面这思路居然 溢出,超时!!!
ActList* ReverseList3(ActList* head) { ActList* p; ActList* q; p=head->next; while(p->next!=NULL){ q=p->next; p->next=q->next; q->next=head->next; head->next=q; } p->next=head;//相当于成环 head=p->next->next;//新head变为原head的next p->next->next=NULL;//断掉环 return head; }
http://blog.csdn.net/feliciafay/article/details/6841115