个人博客网站欢迎交流:萤火之森:https://blog.xkongkeji.com
解题思路:遍历出链表长度,设置一个计数点,当找到倒数第K个节点时返回,链表(感觉走了笨办法)
var getKthFromEnd = function(head, k) {
let conut=1;
let index=1;
let temp=head;
while(temp&&temp.next){
conut++
temp=temp.next
}
while(head){
if((conut-k)+1===index){
return head;
}
if(head.next){
index++;
head=head.next
}else{
head=null
}
}
};