输入一个链表,输出该链表中倒数第k个结点。

public class Solution {
    public ListNode FindKthToTail(ListNode head,int k) {
if (head == null) {
            return null;
        }
        ListNode first = head;
        int length = 0;
        while (first != null) {
            length++;
            first = first.next;
        }
        first = head;
        if (length >= k) {
            for (int i = 0; i < length - k ; i++) {
                first = first.next;
            }
            return first;
        }else {
            return null;
        }
    }
    
}

 

posted @ 2019-03-16 19:58  紫色的雪  阅读(92)  评论(0)    收藏  举报