function node(value) {
this.value = value;
this.next = null;
}
const node1 = new node(1)
const node2 = new node(2)
const node3 = new node(3)
const node4 = new node(4)
const node5 = new node(5)
node1.next = node2;
node2.next = node3;
node3.next = node4;
node4.next = node5;
// 重要 每个节点都认为自己是根节点
function nizhi(root) {
if (root.next.next === null) { // 递归的出口
root.next.next = root; //当前的root 是4 root.next.next ==> 5 5的next ==> 4
return root.next; // 返回的是5的数据
} else {
return nizhi(root.next)
root.next.next = root; // 第一圈 1. next.next ==2 2就会指向1 1的next 必须为null (重要 每个节点都认为自己是根节点 ) 不然互相☞就会死循环了
root.next = null; //关键点 1的next 必须指向null
}
}
const result = nizhi(node1);
console.log(result, 43) // 当前是5
2021-01-15 算法学习=链表逆置
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 链表面试题常用数据结构和技巧 1)使用容器(哈希表、数组等) 2)快慢指针 快慢指针 1)输入链表头节点,奇数长度...