js 单链表的反转
2021-03-19 01:27
标签:def head col sel 反转 one code controls node 思路: js 单链表的反转 标签:def head col sel 反转 one code controls node 原文地址:https://www.cnblogs.com/ajaxkong/p/12041767.html
var reverseList = function (head) {
// 闭包
if (head === undefined || head === null) return null;
var originalHead = head;
var reverseHead;
var reverse = function (head) {
if (head.next === null) {
reverseHead = head;
return head;
} else {
var node = reverse(head.next);
node.next = head;
if (originalHead === head) {
head.next = null;
return reverseHead;
} else {
return head;
}
}
}
return reverse(head)
}