zoukankan      html  css  js  c++  java
  • [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}}}}
  • 相关阅读:
    oracle 判断字符串是否包含指定内容
    java 如何使用多线程调用类的静态方法?
    oracle 快速复制表结构、表数据
    oracle 清空表数据的2种方式及速度比较
    一、Instrument之Core Animation工具
    net登录积分(每天登录积分仅仅能加一次) 时间的比較
    正规方程 Normal Equation
    笑谈贝叶斯网络(干货)上
    SQL SERVER 面试题
    好的创始人想要改变世界,最好的创始人还要不放弃——扎克伯格清华中文演讲
  • 原文地址:https://www.cnblogs.com/Answer1215/p/10575678.html
Copyright © 2011-2022 走看看