Java-14
链表中倒数第k个结点
http://www.nowcoder.com/questionTerminal/886370fe658f41b498d40fb34ae76ff9
倒数第k个节点即正数第n-k+1个节点,先找到第k个节点,剩余 n-k个,再走接下来的,即可得到倒数第k个
import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * public ListNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param pHead ListNode类 * @param k int整型 * @return ListNode类 */ public ListNode FindKthToTail (ListNode pHead, int k) { // write code here if(pHead==null) return null; int index=0; ListNode fast = pHead; ListNode slow = pHead; while(index<k && fast!=null){ fast=fast.next; index++; } if(index<k) return null; while(fast!=null){ fast=fast.next; slow=slow.next; } return slow; } }