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的更多相关文章

  1. *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 ...

  2. [Algorithm] Reverse array of Chars by word

    For example we have: ["p", "r", "e", "f", "e", &qu ...

  3. 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 ...

  4. 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/ ...

  5. [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-> ...

  6. [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-> ...

  7. 【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 ...

  8. 【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-> ...

  9. [LintCode] Reverse Linked List 倒置链表

    Reverse a linked list. Have you met this question in a real interview? Yes Example For linked list 1 ...

随机推荐

  1. Entity Framework 6 (7) vs NHibernate 4: DDD perspective(纯净DDD很难很难...)

    There is quite a bit of Entity Framework vs NHibernate comparisons on the web already, but all of th ...

  2. C#复制数组的两种方式,以及效率比较

    如何高效地进行数组复制? 如果把一个变量值复制给另外一个数组变量,那么2个变量指向托管堆上同一个引用. 如果想在托管堆上创建另外的一份数组实例,通常使用Array.Copy方法. class Prog ...

  3. 淘宝应对"双11"的技术架构分析

    原文地址:http://kb.cnblogs.com/page/193670/ 双“11”最热门的话题是TB ,最近正好和阿里的一个朋友聊淘宝的技术架构,发现很多有意思的地方,分享一下他们的解析资料: ...

  4. UICollectionView 相关

    当数据不多,collectionView.contentSize小于collectionView.frame.size的时候,UICollectionView是不会滚动的 self.Cov.alway ...

  5. C#编程(二十八)----------泛型类的功能

    泛型类的功能 在创建泛型类时,还需要一些其他的C#关键字.例如,不能把null赋予泛型类型.此时,可以使用default关键字.如果泛型类型不需要object类的功能,但需要调用泛型类上的某些特定方法 ...

  6. WordPress主题开发:截取标题或内容

    截取可以用wp_trim_words() 用法: <?php $trimmed = wp_trim_words( $text, $num_words = 55, $more = null ); ...

  7. VS Code搭建.NetCore开发环境(二)

    一.安装VS Code for C#的相关插件 1.C# :https://marketplace.visualstudio.com/items?itemName=ms-vscode.csharp 2 ...

  8. C#与Java 的区别

    相同点:都是面向对象编程的语言,都能够实现面向对象的(封装,继承,多态)思想 不同点:1.   c#中的命名空间是namespace类似于Java中的package(包),在Java中导入包用impo ...

  9. 《成神之路-基础篇》JVM——Java内存模型(已完结)

    Java内存模型 本文是<成神之路系列文章>的第一篇,主要是关于JVM的一些介绍. 持续更新中 Java内存模型 JVM内存结构 VS Java内存模型 VS Java对象模型(Holli ...

  10. [转]微信JSAPI 微信内置JSAPI 2015年1月官方正式API接口,分享完整实例

    FROM : http://www.oschina.net/code/snippet_2276613_45290 HTML通过微信,分享朋友圈出发此JSAPI <?php require_onc ...