SOURCE

/**
 * 将数组转换为链表
 * @param array arr  需要转换的数组
 * @param int  type  转换的类型,0为单链表,1为循环链表
 * @return object    返回链表
 */
function array2List(arr, type = 0) {
  if (!arr.length) return null;
  let header = { index: 0, data:arr[0], next: null };
  let obj = header;
  for (let i = 1; i < arr.length; i++) {
    obj.next = { index: i, data: arr[i], next: null };
    obj = obj.next;
  }
  if (type) obj.next = header;
  return header;
}


function findReverseKNode (head, k) {
  let len = 0;
  let tmp = head;
  let res = head;
  while(tmp) {
    len++;
    tmp = tmp.next;
  }
  if (k >= len) {
    return head;
  }

  for (let i = 0; i < len - k; i++) {
    res = res.next;
  }
  return res;
}
console 命令行工具 X clear

                    
>
console