剑指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) {
        if(head == null){
            return null;
        }
        ListNode P1 = head;
        while(P1 != null && k-- > 0) {
            P1 = P1.next;
        }
        if(k > 0) return null;
        ListNode P2 = head;
        while(P1 != null) {
            P1 = P1.next;
            P2 = P2.next;
        }
        return P2;
    }
}

猜你喜欢

转载自www.cnblogs.com/Roni-i/p/10371960.html