[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 ...
随机推荐
- JS删除String里某个字符的方法
关于JS删除String里的字符的方法,一般使用replace()方法.但是这个方法只会删除一次,如果需要将string里的所以字符都删除就要用到正则. 1 2 3 4 var str = " ...
- [转].net reactor 学习系列(五)---源代码加密程序
.NET Reactor使用教程(加密源代码示例) 1.打开 Eziriz .NET Reactor,主界面如图1所示: 图1 2.单击 Main Assembly 右边的 Open,选择要加密的软件 ...
- .NET Out Of Memory Exception - Used 1.3GB but have 16GB installed
I am getting an Out Of Memory exception in my c# application when the memory usage for the applicati ...
- Objective-C市场占有率排名升至第4位
TIOBE近日公布了2012年4月份的编程语言排行榜,终于不出小编所料,在上个月的编程语言排行榜中说过的“编程语言的王者之争不久很可能会发生改变”实现了,一方面是Java在上几个月中一直属于下滑状态, ...
- winform 给textbox 增加 或 减小字体大小 z
private void btnAddFont_Click(object sender, EventArgs e) { float fSize = this.txtResult.Font.Size; ...
- MySql和相关驱动的安装方式
下载mySql for java驱动的地址:http://www.mysql.com/products/connector/ (可下可不下,因为安装mySql的时候就会包含了各种驱动) MySQL下载 ...
- Kubernetes基础:Pod的详细介绍
本文的演练环境为基于Virtualbox搭建的Kubernetes集群,具体搭建步骤可以参考kubeadm安装kubernetes V1.11.1 集群 1. 基本概念 1.1 Pod是什么 Pod是 ...
- 基于图的图像分割(Graph-Based Image Segmentation)
一.介绍 基于图的图像分割(Graph-Based Image Segmentation),论文<Efficient Graph-Based Image Segmentation>,P. ...
- Doxygen简单经验谈。。。
Doxygen,大名鼎鼎的文档生成工具,被Boost.OpenCasCade等诸多项目作为文档生成的不二人选.人说,才华横溢往往是高深莫测,这句话放在 Doxygen这里显然是不适用的.十八般武艺样样 ...
- 在XP系统中,如何让添加新管理员帐户和原来的管理员帐户同时存在
一.有新账户后administrator账户会自动隐藏的,如果你要用administrator账户登录的话,就机器启动到选账户那里用ctrl+alt+del软启动,然后就可以输入账户名administ ...