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: ...
随机推荐
- MySQL数据库5.7全文索引的坑
1.引擎必须是MyIsAm 2.创建全文索引:ALTER TABLE articles ADD FULLTEXT (title,body); 3.注意全文搜索的字段必须等于或者大于4个字节才会有效 4 ...
- BaseDao+万能方法 , HibernateDaoSupport
package dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStat ...
- idea修改java编译版本
file--Settings project strustructure project strustructure
- C# WPF Border控件总结
Border控件不是一个布局面板,而是一个非常便于使用的元素,经常与布局面板一起使用.所以,在继续介绍其他布局面板之前,现在先介绍一下Border控件是有意义的. Border类非常简单.它只能包含一 ...
- SpringBoot配置文件值植入
<!‐‐导入配置文件处理器,配置文件进行绑定就会有提示‐‐> <dependency> <groupId>org.springframework.boot</ ...
- Nexus2 yum插件RCE漏洞(CVE-2019-5475)
1.漏洞简介: 安全研究员Christian August Holm Hansen在Nexus Repository Manager 2.X中披露了一个远程命令执行漏洞. 该漏洞默认具有部署权限帐户. ...
- git 添加本地项目到远程仓库 记录一下命令
1.初始化 git init 2.关联远程仓库 git remote add origin 你的仓库地址 3.加入到本地仓库 git add * 4.推送(强推).如果不想强推 ,可以先执行下 git ...
- 从零开始,SpreadJS新人学习笔记【第4周】
数据绑定.脏数据和单引号前缀 本周,让我们一起来学习SpreadJS 的数据绑定.脏数据和单引号前缀,希望我的学习笔记能够帮助你们,从零开始学习 SpreadJS,并逐步精通. 在此前的学习笔记中,相 ...
- PLSQL中查到的数据和程序中查询到的不一样
1.首先看下你的修改或者新增的SQL是否提交.
- SVN随笔记录(一)
svn是版本控制系统 为何使用svn? ~团队在开发同一个项目时对项目进行模块划分,在第一阶段结束后进行部分整合时,提交至服务器上合并. ~多人对同一版本的同一代码进行修改后,合并时会出现冲突,此是需 ...