[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 ...
随机推荐
- 使用git pull文件时和本地文件冲突怎么办
在使用git pull代码时,经常会碰到有冲突的情况,提示如下信息:error: Your local changes to 'c/environ.c' would be overwritten by ...
- 初探C++类模版学习笔记
类模板 实现:在定义类的时候给它一个或多个參数,这个些參数表示不同的数据类型. -->抽象的类. 在调用类模板时, 指定參数, 由编译系 ...
- linux设置命令行启动
做spark,内存大的话运行很快,否则运行很慢,主节点用界面,其他用命令行,然后scp,或者ssh 去其他主机最访问 1, 关闭图形界面: [root@bogon ~]# init 3 // 关闭 ...
- rcp(插件开发)点击按钮出现 The chosen operation is not enabled 解决办法
别的项目组,遇到以下错误信息: 首先看一下log日志里的异常信息,估计就知道是什么问题了. 项目组遇到的这个错误是source 指向错误 找不到相关的class.
- Snmp学习总结(五)——WindowsServer2008安装和配置SNMP
一.安装SNMP 在Windows Server 2008以及Windows Server 2008 R2中,SNMP是以一个服务器功能的形式存在的,SNMP的安装步骤如下所示: 1.打开[开始]→[ ...
- Android: INSTALL_FAILED_UPDATE_INCOMPATIBLE错误解决措施
晚上在测一个widget,前面测的好好的,后面再安装的时候发现如下错误:[2009-06-07 02:39:35 - battery] Performing sync[2009-06-07 02:39 ...
- Spark RDD的fold和aggregate为什么是两个API?为什么不是一个foldLeft?
欢迎关注我的新博客地址:http://cuipengfei.me/blog/2014/10/31/spark-fold-aggregate-why-not-foldleft/ 大家都知道Scala标准 ...
- Atlassian JIRA Change IP
Oracle Linux 6.8 Atalssian JIRA 7 原来IP: 192.168.10.200 改新IP: 192.168.12.200 重新跑应用报错,如下所示: 官方提示应用连接不上 ...
- Informix 常用函数
一.内部函数 1.内部合计函数 1)COUNT(*) 返回行数 2)COUNT(DISTINCT COLNAME) 返回指定列中唯一值的个数 3)SUM(COLNAME/EXPRESSION) 返回指 ...
- 泛泰A860(高通8064 cpu 1080p) 刷4.4专用中文recovery TWRP2.7.1.2版(三版通刷)
欢迎关注泛泰非盈利专业第三方开发团队 VegaDevTeam (本team 由 syhost suky zhaochengw(z大) xuefy(大星星) tenfar(R大师) loogeo cr ...