[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 ...
随机推荐
- LPC LINK2 IO CONNECTOR
- C# ie通过打印控件点打印,总是弹出另存为xps的对话框
用的是lodop打印控件,点打印后,总是弹出另存为xps的对话框,后来在网上查到可能是把windows自带的Microsoft XPS Document Writer设为默认打印机的原因. 但现在没有 ...
- EEPLAT学习
eeplat:Open Source Metadata PaaS 開始学习的时候仅仅是下了整个项目而且跑通,顺手在这个平台自己主动化的建立 了个管理系统,接下来開始深入研究这个开源平台 所谓元数据(m ...
- mormot事务控制
mormot事务控制 mormot对所有的数据库类型事务都是一样处理方法gProps.SharedTransaction(1, transBegin);try gProps.SharedTransac ...
- 内存溢出导致jenkins自动部署到tomcat失败
原文地址:http://openwares.net/java/jenkens_deploy_to_tomcat_error_of_outofmemoryerror.html jenkins自动部署wa ...
- 用 setMessage 方法灵活复用 UIAlertView
- tomcat nginx默许的post大小限制
tomcat nginx默认的post大小限制 执行大文件上传,或者,大数据量提交时,当提交的数据大小超过一定限制时,发现后台从request取值的代码request.getParameter(&qu ...
- 解决html5 audio iphone,ipd,safari不能自动播放问题
html audio 在iPhone,ipd,safari浏览器不能播放是有原因滴 (在safri on ios里面明确指出等待用户的交互动作后才能播放media,也就是说如果你没有得到用户的acti ...
- ios成长之每日一遍(day 6)
toolBar上的View Switcher BIDAppDelegate.h #import <UIKit/UIKit.h> @class BIDSwitchViewController ...
- Easyui dialog Y轴滚动条定位
使用Easyui dialog进行数据新增的时候,如果Y轴方向有滚动条,当关闭之前的时候,将滚动条拉到最下方,再次打开的时候,滚动条还是定位在最下方. 需求: 每次打开的时候dialog Y轴滚动条定 ...