[Algorithm] Reverse a linked list
It helps to understands how recursive calls works.
function Node(val) {
return {
val,
next: null
};
}
function LinkedList() {
return {
head: null,
tail: null,
add(val) {
const node = new Node(val);
if (!this.head) {
this.head = node;
this.tail = node;
return node;
}
this.tail.next = node;
this.tail = node;
return node;
},
// 1 - -2 -- x-- x
reverse() {
const helper = node => {
if (!node.next) {
this.head = node;
return;
}
helper(node.next);
// after helper call ends
// node is three
// node.next is four
// swap thre and four and point three next to null
let temp = node.next;
temp.next = node;
node.next = null;
};
return helper(this.head);
}
};
}
const l = new LinkedList();
l.add("one");
l.add("two");
l.add("three");
l.add("four");
l.reverse();
console.log(l.head)
// {"val":"four","next":{"val":"three","next":{"val":"two","next":{"val":"one","next":null}}}}
So for our 'helper' function, when calling it, it stop there until when reach the end.
one |
two |
three |
four |
v
helper()
four |
three |
tow |
one v
To reverse the linked list, everytime we just swap last two node, then set node.next = null.
Here we also should the apporach to using iteration:
function Node(val) {
return {
val,
next: null
};
}
function LinkedList() {
return {
head: null,
tail: null,
add(val) {
const node = new Node(val);
if (!this.head) {
this.head = node;
this.tail = node;
return node;
}
this.tail.next = node;
this.tail = node;
return node;
},
reverse() {
let current = this.head;
let prev = null;
while(current) {
let next = current.next;
current.next = prev;
prev = current;
current = next;
}
this.head = prev;
}
};
}
const l = new LinkedList();
l.add("one");
l.add("two");
l.add("three");
l.add("four");
l.reverse();
console.log(l.head)
// {"val":"four","next":{"val":"three","next":{"val":"two","next":{"val":"one","next":null}}}}
[Algorithm] Reverse a linked list的更多相关文章
- *Amazon problem: 234. Palindrome Linked List (reverse the linked list with n time)
Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...
- [Algorithm] Reverse array of Chars by word
For example we have: ["p", "r", "e", "f", "e", &qu ...
- Data Structure Linked List: Reverse a Linked List in groups of given size
http://www.geeksforgeeks.org/reverse-a-list-in-groups-of-given-size/ #include <iostream> #incl ...
- Data Structure Linked List: Write a function to reverse a linked list
iterative太简单不写了 http://www.geeksforgeeks.org/write-a-function-to-reverse-the-nodes-of-a-linked-list/ ...
- [LeetCode] Reverse Linked List II 倒置链表之二
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...
- [LeetCode] Reverse Linked List II
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...
- 【leetcode】Reverse Linked List II
Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. F ...
- 【leetcode】Reverse Linked List II (middle)
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...
- [LintCode] Reverse Linked List 倒置链表
Reverse a linked list. Have you met this question in a real interview? Yes Example For linked list 1 ...
随机推荐
- DMA Stream/Channel Outputting via GPIOC[0..7]
Ok, so quickly mashing up another example using a different TIM, DMA Stream/Channel for illustration ...
- Android: INSTALL_FAILED_UPDATE_INCOMPATIBLE错误解决措施
晚上在测一个widget,前面测的好好的,后面再安装的时候发现如下错误:[2009-06-07 02:39:35 - battery] Performing sync[2009-06-07 02:39 ...
- TStream实现多表提交
TStream实现多表提交 function TynFiredac.SaveDatas(const ATableName, ATableName2: string; ADeltas: TStream; ...
- shapefile与字符集编码设置
在 ArcGIS Desktop (ArcMap, ArcCatalog, and ArcToolbox) 中,有编码页转换功能(CODE PAGE CONVERSION),可以读写多种字符编码的 s ...
- Ubuntu14.04LTS下使用eclipse搭建Cocos2d-x的Android环境
from://http://www.58player.com/blog-2534-94136.html 最近想玩玩游戏制作,于是选择了目前流行的游戏引擎Cocos2d-x,这个东西虽然有Android ...
- cocos2d-x调用scheduleUpdate()不执行update()方法的解决办法
前两天使用到每帧都更新动画的scheduleUpdate()方法,但通过cclog,我发现, scheduleUpdate()是执行了.但update()方法并没有被调用. 那是因为在CCLayer中 ...
- RTP 有效负载(载荷)类型,RTP Payload Type
转自:http://blog.csdn.net/caoshangpa/article/details/53008018 版权声明:本文为灿哥哥http://blog.csdn.net/caoshang ...
- 在SpringMVC中使用Jackson并格式化时间
在spring MVC 3中,要实现REST风格的JSON服务,最简单的方式是使用 @ResponseBody 注解.该注解会自动把返回的对象,序列化为JSON. 来看一个最简单的例子.这个例子先使用 ...
- 利用svn log命令实现的资源版本更新
无论页游或是手游都需要经常进行更新,而每一次更新几乎都是一部血泪吏.这里重点介绍一下前端资源打包的简化操作.目前2D手游主流都采用了cocos2d-x 绑lua的做法,因为lua相当于一种资源可以进行 ...
- Android之多种Bitmap效果
1. 将图片变为圆角 2. 获取缩略图图片 3. LOMO特效 4. 旧时光特效 5. 暖意特效 6. 根据饱和度.色相.亮度调整图片 7. 添加图片外边框 8. 添加内边框 9. 创建一个缩放的图片 ...