leetCode(1):Reverse Linked List 分类: leetCode 2015-06-16 14:38 161人阅读 评论(0) 收藏

<pre name="code" class="cpp">/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */



ListNode* reverseList(ListNode* head)
{	
	if(!head)
		return head;
	ListNode* p=head;
	ListNode* q=p->next;	
	if(!q)
		return head;
	ListNode* k=q->next;
	p->next=NULL;
	while(k)
	{
		q->next=p;
		p=q;
		q=k;
		k=k->next;
	}	
	q->next=p;
	return q;//返回头结点
}

posted @ 2015-06-16 14:38  朱传林  阅读(125)  评论(0编辑  收藏  举报