47. 反转链表
分享给更多人
容易 -通过 / -执行
前端开发也需要掌握的最基本的算法问题之一 - 反转链表。
假设我们有如下的Node结构
class Node { new(val: number, next: Node); val: number next: Node}
我们把Node节点链接起来,就成了一个链表。
const Three = new Node(3, null)const Two = new Node(2, Three)const One = new Node(1, Two)// 1 → 2 → 3
现在请将其反转,成为 3 → 2 → 1。
你可以修改节点的next
属性,但不能修改val
。
追问
你能在使用递归和不是用递归的情况下完成这个题目吗?