题解 | #输出单向链表中倒数第k个结点#
输出单向链表中倒数第k个结点
https://www.nowcoder.com/practice/54404a78aec1435a81150f15f899417d
快慢指针得到倒数第k个节点的值
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
// 构建节点类
class Node {
    constructor(value, next) {
        this.value = value;
        this.next = next;
    }
}
// 构建单向列表
const create = (arr) => {
    const head = new Node(arr.shift(), null);
    let p = head;
    while (arr[0] != null) {
        let node = new Node(arr.shift(), null);
        p.next = node;
        p = node;
    }
    return head;
};
void (async function () {
    while ((line = await readline())) {
        let n = parseInt(line);
        let arr = (await readline()).split(" ").map(Number);
        let k = Number(await readline());
        let head = create(arr);
        // 快慢指针
        let fast = head;
        let slow = head;
        for (let i = 0; i < k; i++) {
            fast = fast.next;
        }
        while (fast != null) {
            fast = fast.next;
            slow = slow.next;
        }
        console.log(slow.value);
    }
})();
基恩士成长空间 421人发布