反转链表

题目描述

输入一个链表,反转链表后,输出链表的所有元素
/*
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

http://blog.csdn.net/hyqsong/article/details/49429859

http://www.cnblogs.com/byrhuangqiang/p/4311336.html

拥抱明天! 不给自己做枷锁去限制自己。 别让时代的悲哀,成为你人生的悲哀。
原文地址:https://www.cnblogs.com/dd2hm/p/7291247.html