[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}}}}
复制代码

 

posted @   Zhentiw  阅读(144)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2017-03-21 [RxJSv& Javascript] forkJoin (reactive Promise.all) & Map
2017-03-21 [Angular] FormBuildAPI
2017-03-21 [React Router v4] Render Multiple Components for the Same Route
2017-03-21 [React Router v4] Conditionally Render a Route with the Switch Component
2017-03-21 [React Router v4] Render Catch-All Routes with the Switch Component
2017-03-21 [React Router v4] Render Nested Routes
2017-03-21 [React Router v4] Parse Query Parameters
点击右上角即可分享
微信分享提示