Prove safety offer [14] - the inverse of the list of nodes k

Title Description

Input a linked list, the linked list output reciprocal k-th node.

This question my thinking is very simple, is to traverse the list, and then each node pushto a temporary array inside, and finally calculate the reciprocal of the k-th node of indexreturn can be.

function FindKthToTail(head, k)
{
    let values = [];
    while(head){
        values.push(head);
        head = head.next;
    }
    const index = values.length-k;
    return values[index];
}

Guess you like

Origin www.cnblogs.com/Jacob98/p/12465381.html