剑指offer(14)输出链表的倒数第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 node1 = head;
ListNode node2 = head;
if(head == null || k <= 0){
return null;
}
for(int i = 1;i < k;i++){
if(node1.next == null){
return null;
}else{
node1 = node1.next;
}
}
while(node1.next!=null){
node1 = node1.next;
node2 = node2.next;
}
return node2;
}
}