【LeetCode】链表中倒数第k个节点(快慢指针)

题目

思路:快慢指针

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode getKthFromEnd(ListNode head, int k) {
        if(head == null) return null;
        ListNode fast = head, slow = head;
        while(k != 0){
            fast = fast.next;
            k--;
        }
        while( fast != null){
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
}
原文地址:https://www.cnblogs.com/whisperbb/p/12423039.html