js 链表反转 All in One
js 链表反转 All in One
// 链表,节点
function ListNode(val, next) {
this.val = (val === undefined ? 0 : val)
this.next = (next === undefined ? null : next)
}
const head = new ListNode(1, 2);
head.next = new ListNode(2, 3);
head.next.next = new ListNode(3, 4);
head.next.next.next = new ListNode(4, 5);
head.next.next.next.next = new ListNode(5, null);
console.log('head =', head);
// 1 -> 2 -> 3 -> 4 -> 5 -> null
递归
const reverseLinkList = (head) => {
//
return ;
};
const reversedHead = reverseLinkList(head);
console.log('reversed head =', reversedHead);
// 5 -> 4 -> 3 -> 2 -> 1 -> null
迭代
const reverseLinkList = (head) => {
//
return ;
};
const reversedHead = reverseLinkList(head);
console.log('reversed head =', reversedHead);
// 5 -> 4 -> 3 -> 2 -> 1 -> null
refs
©xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/15391436.html
未经授权禁止转载,违者必究!