Single List Reversion
LeetCode
1. 基于头插法的迭代:
public ListNode reverseList(ListNode head) {
if(head == null) return null;
ListNode res = new ListNode(0);
res.next = head;
ListNode cur = head;
while(cur.next != null){
ListNode tmp = cur.next;
cur.next = tmp.next;
tmp.next = res.next;
res.next = tmp;
}
return res.next;
}
2. 就地逆置的简洁迭代(elegant)
public ListNode reverse(ListNode head){
ListNode prev = null;
while(head != null){
ListNode next = head.next;
head.next = prev;
prev = head;
head = next;
}
return prev;
}
3. 递归
ListNode* reverseList(ListNode* head) {
if (!head || !(head -> next)) return head;
ListNode* node = reverseList(head -> next);
head -> next -> next = head;
head -> next = NULL;
return node;
}
Single List Reversion的更多相关文章
- linux-关机出现Telling INIT to go to single user mode.无法关机
运行/sbin/shutdown now最后显示:Telling INIT to go to single user mode.INIT:Going single userINIT:Sending g ...
- [LeetCode] Single Number III 单独的数字之三
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- [LeetCode] Single Number II 单独的数字之二
Given an array of integers, every element appears three times except for one. Find that single one. ...
- [LeetCode] Single Number 单独的数字
Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...
- First,FirstOrDefault,Single,SingleOrDefault的区别
操作符 如果源序列是空的 源序列只包含一个元素 源序列包含多个元素 First 抛异常 返回该元素 返回第一个元素 FirstOrDefault 返回default(TSource) 返回该元素 返回 ...
- 《Single Image Haze Removal Using Dark Channel Prior》一文中图像去雾算法的原理、实现、效果(速度可实时)
最新的效果见 :http://video.sina.com.cn/v/b/124538950-1254492273.html 可处理视频的示例:视频去雾效果 在图像去雾这个领域,几乎没有人不知道< ...
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
- LeetCode——Single Number II(找出数组中只出现一次的数2)
问题: Given an array of integers, every element appears three times except for one. Find that single o ...
- [LeetCode] Single Number
Given an array of integers, every element appears twice except for one. Find that single one. Note: ...
随机推荐
- Eclipse常用快捷方式
Suggestions (Ctrl+1) 建议,比如创建局部变量 File Search (Ctrl+H) 在所有文件中查找关键字 Open Resource (Ctrl+Shift+R) 打开资源 ...
- eclipse搭建简单的web服务,使用tomcat服务
打开eclipse,新建web project, 若本机安装的eclipse版本高,jdk版本低,提示当前版本不适合,解决方法,通过Windows搜索Java,点击配置Java,之后如下图:
- MySQL递归查询父子节点
1.表结构 CREATE TABLE folder( id BIGINT(20) NOT NULL, parent_id BIGINT(20) DEFAULT NULL, PRIMARY KEY id ...
- idea退出提醒 打开
有时候会误点下面的勾选框,导致以后直接退出,没有提示,很不方便,经常误点关闭,再次打开又要等很久 如何设置回来? File-Setting-Appearance&Beha-System Set ...
- Excel随机数相关
基本函数 RAND() 函数:自动生成一个[0,1)的平均分布随机数(依重新计算而改变) RANDBETWEEN(bottom,top) :返回一个介于指定数字直接的随机数,不会自动改变 INT(nu ...
- KVM虚拟化网络管理(4)
一.Linux Bridge网桥管理 网络虚拟化是虚拟化技术中最复杂的部分,也是非常重要的资源.第一节中我们创建了一个名为br0的linux-bridge网桥,如果在此网桥上新建一台vm,如下图: V ...
- 解析之Apache解析
- 切换windows系统输入法的中英文,可以忽视是哪种打字法
调用windows的API //用户获取当前输入法句柄 [DllImport("imm32.dll")] public static extern IntPtr ImmGetCon ...
- Laravel关联模型
public $timestamps = false;//不存时间 1.多对多关联.如收藏.用户表users,产品表products,收藏中间表user_favorite_products.那么在用户 ...
- c++ string详解 assign
assign方法可以理解为先将原字符串清空,然后赋予新的值作替换. 返回类型为 string类型的引用.其常用的重载也有下列几种: a. string& assign ( const stri ...