剑指offer四:链表中倒数第k个结点

输入一个链表,输出该链表中倒数第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 p = head;
ListNode pre = head;
int a =k;
int count = 0; while(p != null){
p = p.next;
count ++;
if(k <1){
pre = pre.next;
}
k--;
}
if(count < a){
return null;
}
return pre;
}
}
上一篇:hdu 3466 Proud Merchants 01背包变形


下一篇:HDU 3466 Proud Merchants(01背包)