xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

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, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2021-10-10 23:34  xgqfrms  阅读(43)  评论(5编辑  收藏  举报