/**
* 输入一个链表,输出该链表中倒数第k个结点。
*/
public class JZ0014FindKthToTail {
public ListNode FindKthToTail(ListNode head, int k) {
if (head == null) {
return null;
}
int count = 1;
ListNode lNode = head;
while (lNode.next != null) {
count++;
lNode = lNode.next;
}
if (k <= 0 || k > count) {
return null;
}
if (k == count) {
return head;
}
if (k == 1) {
return lNode;
}
ListNode pNode = head;
while (k > 1) {
pNode = pNode.next;
k--;
}
while (pNode.next != null) {
pNode = pNode.next;
head = head.next;
}
return head;
}
/*
链表中倒数第K个节点
总结:
1、双指针
2、主要K的范围
*/
}