以下为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;
}