以下为ChatGPT自动生成的解答,正确与否请自行判断。 也许你也可以从大家的发帖中找到想要的答案!

BFE.devAI解答 - JavaScript 代码题
47. 反转链表

非递归解法:

function reverseList(head: Node): Node {  let prev = null;  let curr = head;  while (curr) {    const next = curr.next;    curr.next = prev;    prev = curr;    curr = next;  }  return prev;}

递归解法:

function reverseList(head: Node): Node {  if (!head || !head.next) {    return head;  }  const reversedList = reverseList(head.next);  head.next.next = head;  head.next = null;  return reversedList;}