一、题目
输入一个链表,输出该链表中倒数第k 个结点。为了符合大多数人的习惯,本题从1 开始计数,即链表的尾结点是倒数第1 个结点.例如一个链表有6 个结点,从头结点开始它们的值依次是1 、2、3、4、5 、6。这个个链表的倒数第3 个结点是值为4 的结点。
二、解题思路
为了实现只遍历链表一次就能找到倒数第k 个结点,我们可以定义两个指针。第一个指针从链表的头指针开始遍历向前走k-1步,第二个指针保持不动;从第k 步开始,第二个指针也开始从链表的头指针开始遍历。由于两个指针的距离保持在k-1 , 当第一个(走在前面的)指针到达链表的尾结点时,第二个指针(走在后面的)指针正好是倒数第k 个结点。
public static ListNode findKthToTail(ListNode head, int k) {
if (k < 1 || head == null) {
return null;
}
ListNode pointer = head;
for (int i = 1; i < k; i++) {
if (pointer.next != null) {
pointer = pointer.next;
} else {
return null;
}
}
while (pointer.next != null) {
pointer = pointer.next;
head = head.next;
}
return head;
}
//链表
public class ListNode {
int value;
ListNode next;
public ListNode(int value) {
this.value = value;
}
}
public class MyLinkedList {
public ListNode head;
public ListNode current;
public void add(int data) {
if (head == null) {
head = new ListNode(data);
current = head;
} else {
current.next = new ListNode(data);
current = current.next;
}
}
public ListNode init() {
for (int i = 0; i < 5; i++) {
this.add(i);
}
return head;
}
public void print(ListNode node) {
if (node == null) {
LogUtil.loge("TAG", "node==null");
return;
}
current = node;
while (current != null) {
LogUtil.loge("TAG", "current.value==========" + current.value);
current = current.next;
}
}
}
//打印链表
public static void printNumberNode(String name, ListNode node) {
LogUtil.loge("TAG", name + "::::value::::" + node.value);
}
//调用
public void main(){
MyLinkedList list = new MyLinkedList();
ListNode head = list.init();
list.print(head);
findKthToTail(head,3);
printNumberNode("findKthToTail",findKthToTail(head,3));
}