[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 ...
 
随机推荐
- Optimizing Oracle RAC
			
Oracle Real Application Clusters (RAC) databases form an increasing proportion of Oracle database sy ...
 - STM32 Controller area network (bxCAN) Identifier filtering
			
Identifier filtering In the CAN protocol the identifier of a message is not associated with the addr ...
 - VS2015 打包winform 安装程序
			
最近开发了一个小软件.由于需要打包.网上找了一些资料.然后整合了起来.希望对大家有所帮助.不全面请见谅. 打包控件 InstallShield-Limited-Edition 下面是注册地址 htt ...
 - Revit API过滤管道系统类型
			
管道只能通过PipeType过滤出来类型属性,只能是系统族的类型属性.管道实例过滤不能用族符号和族实例,要用Pipe using System; using System.Collections.Ge ...
 - C# 地磅串口编程
			
C# 地磅串口编程 http://www.cnblogs.com/cancer_xu/archive/2012/09/14/WeighBridge-Com.html http://www.cnblog ...
 - Java嵌入式数据库H2学习总结(一)——H2数据库入门
			
一.H2数据库介绍 常用的开源数据库有:H2,Derby,HSQLDB,MySQL,PostgreSQL.其中H2和HSQLDB类似,十分适合作为嵌入式数据库使用,而其它的数据库大部分都需要安装独立的 ...
 - 在vs2012下编译出现Msvcp120d.dll 丢失的问题
			
之前在vs2012下编译一个opencv程序时,一直出现msvcp120d.dll文件丢失的提示信息,最初会在网上找dll下载,将其拖入系统文件夹再进行regsvr32命令操作,结果都没有解决错误,甚 ...
 - mysql递归查询从子类ID查询所有父类
			
先来看数据表的结构如下: id name parent_id --------------------------- 1 Home 0 2 About ...
 - SharePoint 应用程序页匿名
			
前言 最近,有朋友问开发应用程序页,都是需要先登录再访问,无法开发匿名的应用程序页. 解决方法 其实,SharePoint帮我们提供了匿名访问的应用程序页的方法,只是和普通应用程序页继承的基类不一样, ...
 - Mac iterm2 创建服务器列表