码迷,mamicode.com
首页 > 其他好文 > 详细

leetCode(1):Reverse Linked List

时间:2015-06-16 17:04:14      阅读:88      评论:0      收藏: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;//返回头结点
}

leetCode(1):Reverse Linked List

标签:

原文地址:http://blog.csdn.net/walker19900515/article/details/46518015

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!