Description:输入一个链表,输出该链表中倒数第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) {
if (head == null) return null;
ListNode start = new ListNode(0);
ListNode low = start;
ListNode fast = start;
low.next = head;
for (int i = 1; i <= k; i++) {
fast = fast.next;
}
boolean flag = false;
while (fast != null) {
fast = fast.next;
low = low.next;
flag = true;
}
if (flag)
return low;
return null;
}
}