LEETCODE 206
2016-03-23 22:15
225 查看
Reverse a singly linked list.
click to show more hints.
Hint:
A linked list can be reversed either iteratively or recursively. Could you implement both?
Subscribe to see which companies asked this question
旋转指针,比较简单的题。
/**
* 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) {
ListNode* pre = NULL;
ListNode* nxt = head;
while(nxt)
{
ListNode* temp = nxt->next;
nxt->next = pre;
pre = nxt;
nxt = temp;
}
return pre;
}
};
click to show more hints.
Hint:
A linked list can be reversed either iteratively or recursively. Could you implement both?
Subscribe to see which companies asked this question
旋转指针,比较简单的题。
/**
* 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) {
ListNode* pre = NULL;
ListNode* nxt = head;
while(nxt)
{
ListNode* temp = nxt->next;
nxt->next = pre;
pre = nxt;
nxt = temp;
}
return pre;
}
};
相关文章推荐
- Spring3.1新属性管理API:PropertySource、Environment、Profile
- Quartz源码分析之HelloWorld示例
- 小学生算术能力测试系统
- 类和对象&重载
- MVC框架简谈与在Android中的应用
- codevs 1349 板猪的火车票
- 更新安装xcode7插件
- objective-C学习笔记(五)函数成员:初始化器和析构器
- android 之 启动画面的两种方法
- CentOS 下安装apt-get
- 软件项目管理第三周作业
- php 封装Memcache 队列缓存类
- MyBatis Mapper接口实现原理
- linux c++线程池开发demo
- POJ Mayor's posters (线段树成段更新 离散化666)
- Django--models一对多实例
- RunLoop基础
- Problem A: 删除区间内的元素(线性表)
- NFS – setfacl- quota 配置
- 挖坑——刷gty系列