【編程題】鏈表中倒數第K個結點(Java實現)
題目來源
劍指offer第14題
https://www.nowcoder.com/questionTerminal/529d3ae5a407492994ad2a246518148a
題目描述
輸入一個鏈表,輸出該鏈表中倒數第k個結點。
題目代碼
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
ListNode current=head;
int count=0;
while(current!=null&&head!=null){
count++;
head=head.next;
if(count>k){
current=current.next;
}
}
if(count<k)
return null;
return current;
}
}